Java 25 (LTS) reference · String Methods

Java String split() Method

Split a string around matches of a regular expression and return the parts as an array.

Java 25 (LTS) Since Java 1.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.split(regex, limit)

Parameters

ParameterDescription
regex
String
A regular expression (escape . or | with \\).
limit optional
int
Maximum number of parts.

Return value

A String[].

Examples

Split on commas and whitespace
import java.util.*;

public class Main {
    public static void main(String[] args) {
        String csv = "ana,ben,cleo";
        String[] names = csv.split(",");
        System.out.println(names.length + " names, first: " + names[0]);
        System.out.println(Arrays.toString("a  b   c".split("\\s+")));
    }
}

Output

3 names, first: ana
[a, b, c]

Run this on your computer — see “Get Started” for setup.

Escape dots; limit the parts
import java.util.*;

public class Main {
    public static void main(String[] args) {
        String version = "25.0.5";
        System.out.println(Arrays.toString(version.split("\\.")));   // a plain "." would match every character
        System.out.println(Arrays.toString("k=v=w".split("=", 2)));
    }
}

Output

[25, 0, 5]
[k, v=w]

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 1.0Added

Was this page helpful?