Solved

Java Parsing Strings in Strings

Posted on 2008-10-28
8
247 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

706 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

16 Experts available now in Live!

Get 1:1 Help Now