?
Solved

Java Parsing Strings in Strings

Posted on 2008-10-28
8
Medium Priority
?
287 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 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