dennisk1718
asked on
Java Parsing Strings in Strings
Hello,
Curious issue...
I have a requirement to parse out an incoming string with a string embedded in the string. i.e.
String myString = {"/12345hnd/"a new string"/679hh/"};
where the slashes,"/", are a part of the message and are the key to the parser of a new field in the
stream-of-things. I have tried split() and StreamTokenizer with no success. Since the quotes are an actual part of the message I cannot replace them with a single quote as in " ' " for the recognizer.
Eclipse/Java is not happy with my test setup because it complains bitterly about the string-in-a-string, when I test my Parser.
Curious....
Thank You
Curious issue...
I have a requirement to parse out an incoming string with a string embedded in the string. i.e.
String myString = {"/12345hnd/"a new string"/679hh/"};
where the slashes,"/", are a part of the message and are the key to the parser of a new field in the
stream-of-things. I have tried split() and StreamTokenizer with no success. Since the quotes are an actual part of the message I cannot replace them with a single quote as in " ' " for the recognizer.
Eclipse/Java is not happy with my test setup because it complains bitterly about the string-in-a-string, when I test my Parser.
Curious....
Thank You
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
very interesting question...i like it...
ASKER
Hello,
Yes, Neat...Now some wrinkles...The slashes represent the field delimeters. How to split() along the delimeters while retaining only the escapes of the quoted Strings, so that the quoted strings are also extracted along with the other fields.
Thank You
Yes, Neat...Now some wrinkles...The slashes represent the field delimeters. How to split() along the delimeters while retaining only the escapes of the quoted Strings, so that the quoted strings are also extracted along with the other fields.
Thank You
ASKER
Thank You for the mental prodding...
Further analysis has revealed that the "with wrinkles" solution is to make the spit() regex as follows:
("/"); and include the quoted Strings as escaped.
This permits the use of the slashes as delimeters and provides the quoted string intact.
Further analysis has revealed that the "with wrinkles" solution is to make the spit() regex as follows:
("/"); and include the quoted Strings as escaped.
This permits the use of the slashes as delimeters and provides the quoted string intact.
Just curious why my solution didn't help and what jazz added? Just see jazz saying he liked the question. :) Maybe its just me.
Anyway, yes not a big deal as you found no major wrinkles to end up with "a string".
String myString = "/\"12345hnd\"/\"a new string\"/\"679hh\"";
String[] myValues = myString.split("/");
for(String s : myValues) {
System.out.println(s);
}
ASKER
Thank You