Link to home
Start Free TrialLog in
Avatar of dennisk1718
dennisk1718Flag for United States of America

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
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
very interesting question...i like it...
Avatar of dennisk1718

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
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.
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);
}

Open in new window

Thank You