String.split () - matching leading empty string before first separator?

I need to separate the input string with commas, half-columns or spaces (or a combination of the three). I would also like to handle multiple consecutive input delimiters as a single delimiter. Here is what I still have:

String regex = "[,;\\s]+";    
return input.split(regex);

This works, unless the input line starts with one of the delimiter characters, in which case the first element of the result array is an empty line. I don’t want my result to have empty lines, so something like: ,, ZERO;, ;; ONE, TWO ;, "returns only a three-element array containing header lines.

Is there a better way to do this than to remove any leading characters that match my reg-ex before calling String.split?

Thanks in advance!

+5
source share
4 answers

If “better,” you mean better performance, you can try to create a regular expression that matches what you want to match, and use it Matcher.findin a loop and pulling out matches as they are found. This saves the line change first. But measure it for yourself to find out which is faster for your data.

If “better” you mean simpler, then no, I don’t think there is an easier way than suggested: removing leading separators before applying split.

+3
source

No no. You can ignore only trailing delimiters by providing 0 as the second parameter to the String split () method:

return input.split(regex, 0);

:

return input.replaceFirst("^"+regex, "").split(regex, 0);
+6

, JDK, . , Splitter, , :

Splitter.on(CharMatcher.anyOf(";,").or(CharMatcher.WHITESPACE))
    .omitEmptyStrings()
    .split(",,,ZERO;,ONE TWO");

Iterable <String> , "ZERO", "ONE", "TWO"

+2

StringTokenizer , , :

StringTokenizer st = new StringTokenizer(",,,ZERO;,ONE TWO", ",; ", false);
while(st.hasMoreTokens()) {
  String str = st.nextToken();
  //add to list, process, etc...
}

However, as a warning, you need to define each potential space character separately in the second argument to the constructor.

+1
source

All Articles