I am currently pulling a string from a server, and it has separators between fields, lets just say "\u0003", and I want to split the string at/after the 31st separator, into 2 separate strings. Is such a thing possible? And if yes, how?
-
8Everything's possible! What have you tried so far? How did it work out for you? – Robert Rossmann Jan 06 '15 at 16:47
-
2Yes you can do that using `String.split()` – brso05 Jan 06 '15 at 16:47
-
4please try a bit of research and writing some code before posting the question. – madteapot Jan 06 '15 at 16:50
-
2possible duplicate of [How to split a String in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – madteapot Jan 06 '15 at 16:50
-
The question at the duplicate link explains how to split a string by a separator, but doesn't answer questions of the form "how do you split at the _n_'th occurrence of a separator". – ajb Jan 06 '15 at 16:52
-
ajb hit it on the head, I just did not ask it correctly. I have not tried the usual separator for the string (but I have it throughout the rest of the pages of my app for different things), but this is the only instance where this strange string is what I have to use, and the normal code is probably not the cleanest way to go about it. – tacoman50 Jan 06 '15 at 17:03
-
I think your question is fine, but the title doesn't really get across the point of what you're asking, and I suspect many readers saw the title and immediately went into "this person didn't do any research" mode without reading the whole thing. I edited the title--maybe that will help. – ajb Jan 06 '15 at 17:10
1 Answers
You can do something like this:
Pattern pat = Pattern.compile("((?:[^\u0003]*\u0003){30}[^\u0003]*)\u0003(.*)");
Matcher matcher = pat.matcher(input);
if (matcher.matches()) {
String leftPart = matcher.group(1);
String rightPart = matcher.group(2);
}
The pattern works as follows: The first capture group finds a substring with exactly 30 occurrences of the separator \u0003
; the subpattern that finds a substring with no separators, followed by one separator, is repeated 30 times, and this is then followed by another substring with no separators. This must be followed by a separator (the 31st separator), and the second capture group contains everything that's left in the string. (Note that a parenthesized subpattern that starts with (?:
is a non-capture group and does not count in the numbering used by the group()
method.)
An alternative is to use the split()
method. However, this will require you to reconstruct the left part yourself, by concatenating the first 31 elements of the resulting array (checking first to see if there are actually that many elements), and inserting the separators. Unfortunately, since you're on Android, you can't use the String.join()
method to help with this, since this was added in Java 8 and Android is still stuck somewhere between Java 6 and Java 7.
Note: I've tested this and it works as expected.

- 31,309
- 3
- 58
- 84
-
For anyone curious, I have implemented this code, and it works. Thank you ajb! – tacoman50 Jan 06 '15 at 17:17