String.split() has some very surprising semantics, and it's rarely what you want. You should prefer StringUtils (or Guava's Splitter, discussed in the previous link).
Your specific issue is that String.split() takes a regular expression, while StringUtils.split() uses each character as a separate token. You should use StringUtils.splitByWholeSeparator() to split on the contents of the full string.
StringUtils.splitByWholeSeparator(str, "__|__");
Answer from dimo414 on Stack OverflowString.split() has some very surprising semantics, and it's rarely what you want. You should prefer StringUtils (or Guava's Splitter, discussed in the previous link).
Your specific issue is that String.split() takes a regular expression, while StringUtils.split() uses each character as a separate token. You should use StringUtils.splitByWholeSeparator() to split on the contents of the full string.
StringUtils.splitByWholeSeparator(str, "__|__");
No, as per documentation, second parameter of StringUtils.split is the list of all characters that are considered splitters. There is a different function in Apache Commons which does what you want - StringUtils.splitByWholeSeparator. Still, I don't get what's wrong with simple String.split.
StringUtils uses any of the characters in the separatorChars argument as the separator, not necessarily the whole thing. The javadoc also states
The separator is not included in the returned String array. Adjacent separators are treated as one separator.
Parameters
- separatorChars the characters used as the delimiters, null splits on whitespace
Alternatively, you can use StringUtils.splitByWholeSeparator(String, String) to split on the exact @--@ or whatever it is.
In this scenario it takes both @,- as separator so thats why this output is produced. For exact output you have to try some other "separator"
To Refer : http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#split%28java.lang.String,%20java.lang.String%29
You can use this version of split, for example
String[] strings = StringUtils.split("some,random words", ", ");
or the built-in split method (as per my comment)
String[] strings = "some,random words".split("[, ]")?
String.split() accepts a regex expression, so you can use:
"test,string split".split("[, ]")
EDIT: Just noticed Reimeus already mentioned this.
The method you are looking for is StringUtils.splitByWholeSeparatorPreserveAllTokens():
String result[] = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, "||");
You could also achieve the same result with standard Java and without library with String.split() and a regular expression:
String result[] = input.split("\\|\\|");
The documentation says, that adjacent separators are treated as one separator
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#split(java.lang.String, java.lang.String)
Try using StrTokenizer instead
String input = "A||B||||D||E";
StrTokenizer tokenizer = new StrTokenizer(input, "||");
tokenizer.setIgnoreEmptyTokens(false);
for (int i = 0; i < tokenizer .getTokenArray().length; i++) {
System.out.println("result[" + i + "]:" + tokenizer.getTokenArray()[i]);
}