In Java, split(regex) cuts the string at every match of the regular expression regex and returns the pieces in a new String[]. The delimiters themselves are not part of the result, and the original string is unchanged.
The argument is a regex, not plain text, so "\\s+" splits on any run of whitespace while a bare "." matches every character. By default trailing empty strings are removed; the limit overload controls that and the maximum number of pieces.
split() is an instance method of java.lang.String, added in Java 1.4. Call it on a string, as in text.split(...); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.
Syntax
Pass a regex, and optionally a limit.
java
String[] cells = row.split(","); // trailing empty cells dropped
String[] all = row.split(",", -1); // keep every cell
String[] kv = line.split("=", 2); // at most 2 pieces
Call
What it does
"a,b,c".split(",")
[a, b, c].
"a,,b,".split(",")
[a, , b]: the trailing empty piece is gone.
"a,,b,".split(",", -1)
[a, , b, ].
"abc".split(";")
[abc]: no match, one piece.
The limit works like this: limit > 0 gives at most limit pieces (the last one holds the rest of the text); limit == 0 (the default) removes trailing empty strings; limit < 0 keeps every piece.
Library declarations
java
public String[] split(String regex);
public String[] split(String regex, int limit);
Parameters
Parameter
Description
regex
The delimiter as a regular expression.
limit
Optional. Maximum number of pieces (positive), keep all (negative), or drop trailing empties (0).
Return Value
Return Value
Description
String[]
The pieces between matches. When nothing matches, an array containing the whole string.
Java Examples
Example 1 — Split a CSV Row With and Without a Limit
Empty cells at the end vanish unless you pass a negative limit.
java
import java.util.Arrays;
public class SplitCsv {
public static void main(String[] args) {
String row = "a,b,,c,,";
String[] parts = row.split(",");
String[] all = row.split(",", -1);
System.out.println(parts.length + " " + Arrays.toString(parts));
System.out.println(all.length + " " + Arrays.toString(all));
}
}
Output
4 [a, b, , c]
6 [a, b, , c, , ]
How It Works
Empty cells in the middle stay. The ",," between b and c produces an empty string in both arrays.
Trailing empties are dropped. With the default limit 0, the two empty cells after c are removed, leaving 4 pieces.
A negative limit keeps all.split(",", -1) returns all 6 cells, which matters when every row must have the same number of columns.
Example 2 — Split on Whitespace and on a Literal Dot
A regex makes some splits easy and others surprising.
java
import java.util.Arrays;
public class SplitRegex {
public static void main(String[] args) {
String sentence = " split on\tany space ";
System.out.println(Arrays.toString(sentence.trim().split("\\s+")));
String version = "1.2.3";
System.out.println(version.split(".").length);
System.out.println(Arrays.toString(version.split("\\.")));
}
}
Output
[split, on, any, space]
0
[1, 2, 3]
How It Works
Runs of whitespace. The regex \s+ treats several spaces or a tab as one delimiter; trimming first avoids an empty first piece.
The dot trap."." matches every character, so all pieces are empty, and removing trailing empty strings leaves an array of length 0.
Escape to split on a period."\\." matches only the literal dot and gives the three version numbers.
Example 3 — Split Key=Value Pairs with a Limit of 2
Values may contain the delimiter too; a limit keeps them in one piece.
java
public class SplitLimit {
public static void main(String[] args) {
String[] lines = {"name=Ada", "url=https://x.dev/?a=1", "empty="};
for (String line : lines) {
String[] kv = line.split("=", 2);
System.out.println(kv[0] + " -> [" + kv[1] + "]");
}
}
}
Output
name -> [Ada]
url -> [https://x.dev/?a=1]
empty -> []
How It Works
At most two pieces. With limit = 2, only the first = splits the line; the rest stays in the second piece.
Delimiters inside the value survive. The URL keeps its own = because splitting stopped after one cut.
Empty values are kept. A positive limit does not drop trailing empty strings, so "empty=" safely yields kv[1] == "".
Frequently Asked Questions
With the default limit of 0, trailing empty strings are removed. Pass a negative limit, such as split(",", -1), to keep them.
The argument is a regex and . matches any character, so every piece is empty and all of them are removed. Use split("\\.") instead.
Use text.trim().split("\\s+"). The regex \s+ matches any run of spaces, tabs, or line breaks.
A positive limit caps the number of pieces, 0 drops trailing empty strings, and a negative limit keeps every piece.
Escape it with a backslash, for example split("\\|"), or use split(Pattern.quote("|")).