I have this string (Java 1.5):
:alpha;beta:gamma;delta
I need to get an array:
{\":alpha\", \";beta\", \":gamma\", \";delta
This should work with Java 1.5 (Pattern.quote was introduced in Java 1.5).
// Split the string on delimiter, but don't delete the delimiter
private String[] splitStringOnDelimiter(String text, String delimiter, String safeSequence){
// A temporary delimiter must be added as Java split method deletes the delimiter
// for safeSequence use something that doesn't occur in your texts
text=text.replaceAll(Pattern.quote(delimiter), safeSequence+delimiter);
return text.split(Pattern.quote(safeSequence));
}
If first element is the problem:
private String[] splitStringOnDelimiter(String text, String delimiter, String safeSequence){
text=text.replaceAll(Pattern.quote(delimiter), safeSequence+delimiter);
String[] tempArray = text.split(Pattern.quote(safeSequence));
String[] returnArray = new String[tempArray.length-1];
System.arraycopy(tempArray, 1, returnArray, 0, returnArray.length);
return returnArray;
}
E.g., here "a" is the delimiter:
splitStringOnDelimiter("-asd-asd-g----10-9asdas jadd", "a", "<>")
You get this:
1.: -
2.: asd-
3.: asd-g----10-9
4.: asd
5.: as j
6.: add
If you in fact want this:
1.: -a
2.: sd-a
3.: sd-g----10-9a
4.: sda
5.: s ja
6.: dd
You switch:
safeSequence+delimiter
with
delimiter+safeSequence