Solved

Java Parsing Strings in Strings

Posted on 2008-10-28
8
250 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
  • 3
  • 3
8 Comments
 
LVL 59

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 59

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 59

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Securing Jmx Console and web console 2 65
print map entry 34 55
replace a word with other 1 40
servlet web applications   metadata-complete="true" or false 3 0
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now