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 Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_split.asp
Java String split() Method
The split() method splits a string into an array of substrings using a regular expression as the separator. If a limit is specified, the returned array will not be longer than the limit.
Discussions

Java- Converting a string of numbers separated by spaces into an array?
You have the right idea. String.split() will return an array of strings. Then, if you want to treat any individual string in that array as an integer you can use Integer.parseInt() . If you want to create an array of integers, just make the array that way. More on reddit.com
๐ŸŒ r/learnprogramming
7
4
February 2, 2016
String.split into a string array
I am tying to split a list of items each on a new line. the code i am using is varriable lines default to text.split(โ€œ\r\nโ€) How ever I am getting an error that "option strict on disallows implicit conversions from โ€˜stringโ€™ to โ€˜charโ€™. I have tried slapping a toarray and tostring ... More on forum.uipath.com
๐ŸŒ forum.uipath.com
0
March 11, 2020
Java String to Array split
Array[r++] = row.split("\\|"); that won't even compile, as "Array" (horrible variable name btw) isn't defined. Did you try googling this? there's a zillion tutorials about how to split strings... also: r/javahelp . More on reddit.com
๐ŸŒ r/java
1
0
February 8, 2013
Splitting an array of strings in Java using .split - Stack Overflow
I have browsed through questions similar to mine already on here, but have not found anything that will work for me. Essentially what I have to do is read a file full of strings line by line, print More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Quora
quora.com โ€บ How-do-you-split-a-string-and-store-it-in-an-array-in-Java
How to split a string and store it in an array in Java - Quora
In java. lang. String. split(String regex, int limit) method splits this string around matches of the given regular expression. The array returned by this method contains each substring of...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ java string.split()
Java.String.split() | Baeldung
March 13, 2025 - We can also pass a limit on the number of splits to the split() method. The limit determines how many times the string will be split: If the limit is greater than 0, the string is split at most limit โ€“ 1 times.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ java- converting a string of numbers separated by spaces into an array?
r/learnprogramming on Reddit: Java- Converting a string of numbers separated by spaces into an array?
February 2, 2016 -

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!

๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ strings โ€บ .split()
Java | Strings | .split() | Codecademy
March 30, 2022 - The .split() method takes a string and splits it into an array of substrings based on a pattern delimiter. The pattern provided is a regular expression that will split the original string based on where the pattern is matched in the string.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-use-space-together-and-split-a-string-into-an-array-in-Java
How to use space together and split a string into an array in Java - Quora
Answer: Here is a way to use space together and split a string into an array in Java You can split a String by whitespaces or tabs in Java by using the split() method of java.lang.String class.
Find elsewhere
๐ŸŒ
UiPath Community
forum.uipath.com โ€บ help โ€บ studio
String.split into a string array - Studio - UiPath Community Forum
March 11, 2020 - I am tying to split a list of items each on a new line. the code i am using is varriable lines default to text.split(โ€œ\r\nโ€) How ever I am getting an error that "option strict on disallows implicit conversions from โ€˜strโ€ฆ
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ convert string to array in java
String to Array in Java - Scaler Topics
April 19, 2024 - Example 2: We changed the string to array in Java in the following example using the # delimiter. ... The Pattern.split() method uses a regular expression(pattern) as the delimiter to split a string into an array of strings.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ string-to-array-in-java-how-to-convert-a-string-to-an-array-in-java
String to Array in Java โ€“ How to Convert Strings to Arrays
July 6, 2023 - Performance: Creating a new character ... especially for long strings. The split() method in Java is a convenient way to split a string into an array of substrings based on a specified delimiter....
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ how to split a string in java
How to split a string in Java | Sentry
May 15, 2023 - public class Main { public static void main(String[] arg) { String str = "how.to.split.a.string.in.java"; String[] arrOfStr = str.split("\\."); for (String a : arrOfStr) { System.out.println(a); } } } This example also has the same output as the previous examples. If the split character is not present in the target string, the output will be an array with one element containing the entire string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ split-string-java-examples
Java String split() Method - GeeksforGeeks
December 20, 2025 - split() method in Java is used to divide a string into an array of substrings based on a specified delimiter or regular expression.
๐ŸŒ
Briebug Blog
blog.briebug.com โ€บ blog โ€บ java-split-string
Using the Java String.split() Method
October 13, 2022 - The Java String.split() method has two variations, commonly known as method overloading, which are both used to split a String into an array of Strings, using a passed delimiter or regular expression.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ split-string-by-newline-in-java
Split String by Newline in Java
September 21, 2022 - You can call the split() method to split string by newline in Java 8 or higher like this: ... In Java 11, a new method lines() has been added to the String class. This method internally uses \R pattern to match the newline of the underlying operating system. Stream<String> str = "Text\nTex...
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ java string to array split
r/java on Reddit: Java String to Array split
February 8, 2013 -

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("\\|");
}}
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 521224 โ€บ java โ€บ String-split
String split - Beginning Java
December 22, 2010 - Jump straight into any of our topics and light hearted discussions. Ranging from java, databases, android, programmer certification, programming jobs and much more... Sign up/login now!
Top answer
1 of 4
22

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)

2 of 4
6

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
}