This behavior is explicitly documented in String.split(String regex) (emphasis mine):
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
If you want those trailing empty strings included, you need to use String.split(String regex, int limit) with a negative value for the second parameter (limit):
String[] array = values.split("\\|", -1);
Answer from Mark Rotteveel on Stack OverflowThis behavior is explicitly documented in String.split(String regex) (emphasis mine):
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
If you want those trailing empty strings included, you need to use String.split(String regex, int limit) with a negative value for the second parameter (limit):
String[] array = values.split("\\|", -1);
Try this
String[] array = values.split("\\|",-1);
java - Split String into an array of String - Stack Overflow
java - Split string into array of character strings - Stack Overflow
Using charAt(index) method to split a String into an array.
Split a string with multiple delimiters
Erm, you can just put all of the delimiters you want in the quotes for the string passed to .split()
[PS] C:\Users\> $string = "part1.part2;part3"
[PS] C:\Users\> $string.split(".;")
part1
part2
part3 More on reddit.com Videos
You need a regular expression like "\\s+", which means: split whenever at least one whitespace is encountered. The full Java code is:
try {
String[] splitArray = input.split("\\s+");
} catch (PatternSyntaxException ex) {
//
}
String[] result = "hi i'm paul".split("\\s+"); to split across one or more cases.
Or you could take a look at Apache Common StringUtils. It has StringUtils.split(String str) method that splits string using white space as delimiter. It also has other useful utility methods