Solved

Java Parsing Strings in Strings

Posted on 2008-10-28
8
264 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:dennisk1718
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 22825914
You need to escape the double quotes (").  

You will have to do something like this:
String myString = "\"/12345hnd\"/a new string\"/679hh\"/";
		
String[] myValues = myString.split("\"/");
 
for(String s : myValues) {
    System.out.println(s);
}

Open in new window

0
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 22826107
very interesting question...i like it...
0
 

Author Comment

by:dennisk1718
ID: 22826345
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dennisk1718
ID: 22826753
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.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22826804
Just curious why my solution didn't help and what jazz added?  Just see jazz saying he liked the question. :) Maybe its just me.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22826817
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

0
 

Author Closing Comment

by:dennisk1718
ID: 31510921
Thank You
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question