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- Converting a string of numbers separated by spaces into an array?
String.split into a string array
Java String to Array split
Splitting an array of strings in Java using .split - Stack Overflow
Videos
For example, I have String input which will look something like "5 6 6" but I need it to be converted into an array. What is the easiest way to do this? I have messed around with .split and .parseInt but have not come to any solid conclusions. Thanks!
Hi, I'm still learning Java and I cannot figure out how to split a String into multidimensional Array. This is my sample Array
private static String[][] Array = new String[][] {
{ "0.00", "0.00", "0.00", "", "0" },
{ "item1", "item 2", "2", "item4", "5" },
{ "0", "0", "0", "0", "0" }};This is how my string looks that I want to input to my array
0.00|0.00|0.00||0|item1|item 2|2|item4|5|0|0|0|0|0|
I was able to split the string to using only one column and multiple rows but I am having trouble splitting the string to multiple columns using only 5 rows. I java tries using loop inside a loop but I keep getting type mismatch error
public static void stringToArray(String string) {
String str = string;
String[] rows = str.split(",");
int r = 0;
for (String row : rows) {
Array[r++] = row.split("\\|");
}}Try something like this:
static public void printWords(String [ ] arr)
{
for(String s : arr) {
String[] s2 = s.split(" ");
for(String results : s2) {
System.out.println(results);
}
}
}
With this you don't need the count variable either.
it could be an error with your for loop stopping at count instead of (array.length || count)
I've written a quick and dirty benchmark test for this. It compares 7 different methods, some of which require specific knowledge of the data being split.
For basic general purpose splitting, Guava Splitter is 3.5x faster than String#split() and I'd recommend using that. Stringtokenizer is slightly faster than that and splitting yourself with indexOf is twice as fast as again.
For the code and more info see https://web.archive.org/web/20210613074234/http://demeranville.com/battle-of-the-tokenizers-delimited-text-parser-performance (original link is dead and corresponding site does not appear to exist anymore)
As @Tom writes, an indexOf type approach is faster than String.split(), since the latter deals with regular expressions and has a lot of extra overhead for them.
However, one algorithm change that might give you a super speedup. Assuming that this Comparator is going to be used to sort your ~100,000 Strings, do not write the Comparator<String>. Because, in the course of your sort, the same String will likely be compared multiple times, so you will split it multiple times, etc...
Split all the Strings once into String[]s, and have a Comparator<String[]> sort the String[]. Then, at the end, you can combine them all together.
Alternatively, you could also use a Map to cache the String -> String[] or vice versa. e.g. (sketchy) Also note, you are trading memory for speed, hope you have lotsa RAM
HashMap<String, String[]> cache = new HashMap();
int compare(String s1, String s2) {
String[] cached1 = cache.get(s1);
if (cached1 == null) {
cached1 = mySuperSplitter(s1):
cache.put(s1, cached1);
}
String[] cached2 = cache.get(s2);
if (cached2 == null) {
cached2 = mySuperSplitter(s2):
cache.put(s2, cached2);
}
return compareAsArrays(cached1, cached2); // real comparison done here
}