DTwined
asked on
Parse String
Hello -
I'm looking for a good and efficient way to parse the following string. I want to loop and pull all the numbers out of the string. Please note there is a space before and after the number.
"AAA: 111 BBB: 222 CCC: 333 DDD: 444 EEE: 555"
thanks!
DT
I'm looking for a good and efficient way to parse the following string. I want to loop and pull all the numbers out of the string. Please note there is a space before and after the number.
"AAA: 111 BBB: 222 CCC: 333 DDD: 444 EEE: 555"
thanks!
DT
ASKER
Looks good and you get the points but I screwed up and left one thing out.
What if one of the values I'm trying to pull has a comma, is there a way I can ignore it.
For example:
String ssline = "AAA: 111 BBB: Smith, Bill CCC: 333 DDD: 444 EEE: 555"
I need to keep "Smith, Bill" together.
thanks!
What if one of the values I'm trying to pull has a comma, is there a way I can ignore it.
For example:
String ssline = "AAA: 111 BBB: Smith, Bill CCC: 333 DDD: 444 EEE: 555"
I need to keep "Smith, Bill" together.
thanks!
commaa is not a problem,
see below:
ss111
ss222
ss333
ss444
ss555
WAht is a problem is that I acnnot get away from the first empty line - i just ignored it startig form index 1
but if it starts from the number you need not to ignore it
see below:
String ssline = "AAA: 111 BBB: 222 CCC: 333 D,DD: 444 EEE: 555";
String [] sslines = ssline.split("[\\D,]+");
for(int j=1; j<sslines.length; j++){
System.out.println( "ss" + sslines[j]);
}
ss111
ss222
ss333
ss444
ss555
WAht is a problem is that I acnnot get away from the first empty line - i just ignored it startig form index 1
but if it starts from the number you need not to ignore it
Thsi would work for all lines
if they start from number or not
but still I'm unhappy
if they start from number or not
String ssline = "AAA: 111 BBB: 222 CCC: 333 D,DD: 444 EEE: 555";
String [] sslines = ssline.split("[\\D,]+");
for(int j=0; j<sslines.length; j++){
if(j==0 && sslines[j].trim().length() == 0)continue;
System.out.println(sslines[j]);
}
Output:111
222
333
444
555
but still I'm unhappy
ASKER
Thanks. It's still not doing exactly what I need. My fault, I should have not just done all numbers. Could you get it to parse my example string. There may be cases where there are alphabetic chars after the colon.
String ssline = "AAA: 111 BBB: Smith, Bill CCC: 333 DDD: 444 EEE: 555"
I'd like to get back the following:
111
Smith, Bill
333
444
555
thanks!
String ssline = "AAA: 111 BBB: Smith, Bill CCC: 333 DDD: 444 EEE: 555"
I'd like to get back the following:
111
Smith, Bill
333
444
555
thanks!
tehn formulate - what do you want to remove?
what is the dofference between say "BBB" and "Smith" ?
We can split by colon, but how do we know what to remove -
llike say all words ending with colon?
what is the dofference between say "BBB" and "Smith" ?
We can split by colon, but how do we know what to remove -
llike say all words ending with colon?
ASKER
Yeah, I need to pull the value after each colon.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
This would be still better, as it dowes not ignore first empty string,
but rather splits withourt it
Output:
but rather splits withourt it
String ssline = "AAA: 111 BBB: Smith, Bill CCC: 333 DDD: 444 EEE: 555";
String [] sslines = ssline.replaceFirst("^[A-Z]+:","").split("[A-Z]+:");
for(int j=0; j<sslines.length; j++){
System.out.println(sslines[j].trim());
}
Output:
111
Smith, Bill
333
444
555
ASKER
Thanks. One more question what if the letters before the colon were a mix of Upper and Lower case?
for example:
"Aaa: 111 Bbb: Smith, Bill Ccc: 333 Ddd: 444 Eee: 555";
thanks!
for example:
"Aaa: 111 Bbb: Smith, Bill Ccc: 333 Ddd: 444 Eee: 555";
thanks!
String ssline = "AAA: 111 Bbb: Smith, Bill CCC: 333 DDD: 444 EEE: 555";
String [] sslines = ssline.replaceFirst("^[A-Za-z]+:","").split("[A-Za-z]+:");
for(int j=0; j<sslines.length; j++){
System.out.println(sslines[j].trim());
}
111
Smith, Bill
333
444
555
ASKER
Awesome! Works great!
Open in new window
output:
Open in new window