In Java, what is the best way to split a string into an array of blocks, when the delimiters at the beginning of each block are different from the delimiters at the end of each block?
For example, suppose I have String string = "abc 1234 xyz abc 5678 xyz"
.
I want to apply some sort of complex split
in order to obtain {"1234","5678"}
.
The first thing that comes to mind is:
String[] parts = string.split("abc");
for (String part : parts)
{
String[] blocks = part.split("xyz");
String data = blocks[0];
// Do some stuff with the 'data' string
}
Is there a simpler / cleaner / more efficient way of doing it?
My purpose (as you've probably guessed) is to parse an XML document.
I want to split a given XML string into the Inner-XML blocks of a given tag.
For example:
String xml = "<tag>ABC</tag>White Spaces Only<tag>XYZ</tag>";
String[] blocks = Split(xml,"<tag>","</tag>"); // should be {"ABC","XYZ"}
How would you implement String[] Split(String str,String prefix,String suffix)
?
Thanks