Java String split() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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
CallWhat 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

ParameterDescription
regexThe delimiter as a regular expression.
limitOptional. Maximum number of pieces (positive), keep all (negative), or drop trailing empties (0).

Return Value

Return ValueDescription
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));
    }
}

How It Works

  1. Empty cells in the middle stay. The ",," between b and c produces an empty string in both arrays.
  2. Trailing empties are dropped. With the default limit 0, the two empty cells after c are removed, leaving 4 pieces.
  3. 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("\\.")));
    }
}

How It Works

  1. Runs of whitespace. The regex \s+ treats several spaces or a tab as one delimiter; trimming first avoids an empty first piece.
  2. The dot trap. "." matches every character, so all pieces are empty, and removing trailing empty strings leaves an array of length 0.
  3. 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] + "]");
        }
    }
}

How It Works

  1. At most two pieces. With limit = 2, only the first = splits the line; the rest stays in the second piece.
  2. Delimiters inside the value survive. The URL keeps its own = because splitting stopped after one cut.
  3. 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("|")).

Next: splitWithDelimiters()

Split around a regex and keep the delimiters (Java 21+).

splitWithDelimiters() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful