Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This might be a simpler way of asking the question.

I am trying to split a string twice
XML
String example = response;
        String [] array = example.split("<section>");
        System.out.println(array[0]);

        String [] array2 = example.split("<title>");
        System.out.println(array2[2]);


I was trying to achieve this by using this code (not successfully), but instead of printing the first split I would like to save it and carry on with the 2nd split.
Would anyone have a solution to this problem or better way of going about splitting a string twice?
Posted
Updated 29-Dec-13 9:04am
v6
Comments
GuyThiebaut 29-Dec-13 14:01pm    
I don't understand what it is you are trying to achieve.

Could you post what the string reponse might contain and what you expect to be returned in your first and second println commands.
Thanks
Member 10493383 29-Dec-13 14:13pm    
hope this help in any way.thanks
Sergey Alexandrovich Kryukov 29-Dec-13 14:19pm    
[Posted my mistake... — SA]
Sergey Alexandrovich Kryukov 29-Dec-13 14:52pm    
Sorry, probably I misplaced my post, please ignore my comment, sorry for the inconvenience... :-)
—SA
Sergey Alexandrovich Kryukov 29-Dec-13 14:55pm    
It depends on what you wanted to achieve. I can understand why this way may complicate things. How about splitting my one set of delimiters, and then splitting the result of first split? Or split by a complex Regex..?
—SA

Please see my comment to the question where I advice to split in some different ways. But it looks like your source string is XML, so, in most cases, you should rather use an XML parser and parse all you have at once. You can use javax.xml.xpath:
http://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/package-summary.html[^].

For complete documentation on XML parsing, please see: http://docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.htm[^].

Good luck, best wishes in New Year!
—SA
 
Share this answer
 
Comments
Maciej Los 29-Dec-13 16:25pm    
+5!
Parser instead Splitter ;)
Sergey Alexandrovich Kryukov 29-Dec-13 16:35pm    
Thank you, Maciej.
—SA
Maciej Los 29-Dec-13 16:41pm    
You're very welcome ;)
Could you look at mine answer?
OP wrote:
trying to split a string twice
Java
String example = response;
 String [] array = example.split("<section>");
 System.out.println(array[0]);

 String [] array2 = example.split("<title>");
 System.out.println(array2[2])



If i understand you well, you're trying to split some text into parts based on different condition. Firtly, you want to devide text on "<section>" then (secondly) on "<title>" statement.
Now, have a look at mine pseudo-code (i'm not familiar with java enough):
Java
String [] array = example.split("<section>");
System.out.println(array[0]);
//for each part in a first array
for(int i=0,array[].UBound(),i++)
{
    String [] array2 = array[i].split("<title>");
}


Do you see the difference?
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 29-Dec-13 16:53pm    
Isn't that exactly the same I advised in my comment to the question and hour ago?
—SA
Maciej Los 29-Dec-13 17:19pm    
Oops.. I missed your last comment to OP's question...
You're right. I think, we can treat this answer as a support to your last comment ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900