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
Syntax
string.split(regex, limit)
Parameters
| Parameter | Description |
|---|---|
regexString |
A regular expression (escape . or | with \\). |
limit optionalint |
Maximum number of parts. |
Return value
A String[].
Examples
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.
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
| Version | Change |
|---|---|
| Java Java 1.0 | Added |
Related
String.join()· Join strings or a collection of strings into one string, with a separator between them.string.substring()· Return part of a string from a start index up to (not including) an end index. The original is unchanged.- Java Strings · tutorial