The split() method divides the string at the specified regex and returns an array of substrings.
Example
class Main {
public static void main(String[] args) {
String text = "Java is a fun programming language";
// split string from space
String[] result = text.split(" ");
System.out.print("result = ");
for (String str : result) {
System.out.print(str + ", ");
}
}
}
// Output: result = Java, is, a, fun, programming, language,
Syntax of String split()
The syntax of the string split() method is:
string.split(String regex, int limit)
Here, string is an object of the String class.
split() Parameters
The string split() method can take two parameters:
- regex - the string is divided at this regex (can be strings)
- limit (optional) - controls the number of resulting substrings
If the limit parameter is not passed, split() returns all possible substrings.
split() Return Value
- returns an array of substrings
Note: If the regular expression passed to split() is invalid, the split() method raises PatternSyntaxExpression exception.
Example 1: split() Without limit Parameter
// importing Arrays to convert array to string
// used for printing arrays
import java.util.Arrays;
class Main {
public static void main(String[] args) {
String vowels = "a::b::c::d:e";
// splitting the string at "::"
// storing the result in an array of strings
String[] result = vowels.split("::");
// converting array to string and printing it
System.out.println("result = " + Arrays.toString(result));
}
}
Output
result = [a, b, c, d:e]
Here, we split the string at ::. Since the limit parameter is not passed, the returned array contains all the substrings.
split() With limit Parameter
- If the
limitparameter is 0 or negative,split()returns an array containing all substrings. - If the
limitparameter is positive (let's sayn),split()returns the maximum ofnsubstrings.
Example 2: split() With limit Parameter
// importing Arrays to convert array to string
import java.util.Arrays;
class Main {
public static void main(String[] args) {
String vowels = "a:bc:de:fg:h";
// splitting array at ":"
// limit is -2; array contains all substrings
String[] result = vowels.split(":", -2);
System.out.println("result when limit is -2 = " + Arrays.toString(result));
// limit is 0; array contains all substrings
result = vowels.split(":", 0);
System.out.println("result when limit is 0 = " + Arrays.toString(result));
// limit is 2; array contains a maximum of 2 substrings
result = vowels.split(":", 2);
System.out.println("result when limit is 2 = " + Arrays.toString(result));
// limit is 4; array contains a maximum of 4 substrings
result = vowels.split(":", 4);
System.out.println("result when limit is 4 = " + Arrays.toString(result));
// limit is 10; array contains a maximum of 10 substrings
result = vowels.split(":", 10);
System.out.println("result when limit is 10 = " + Arrays.toString(result));
}
}
Output
result when limit is -2 = [a, bc, de, fg, h] result when limit is 0 = [a, bc, de, fg, h] result when limit is 2 = [a, bc:de:fg:h] result when limit is 4 = [a, bc, de, fg:h] result when limit is 10 = [a, bc, de, fg, h]
Note: The split() method takes regex as the first argument. If you need to use special characters such as: \, |, ^, *, + etc, you need to escape these characters. For example, we need to use \\+ to split at +.
Example 3: split() at the + character
// importing Arrays to convert array to string
// used for printing arrays
import java.util.Arrays;
class Main {
public static void main(String[] args) {
String vowels = "a+e+f";
// splitting the string at "+"
String[] result = vowels.split("\\+");
// converting array to string and printing it
System.out.println("result = " + Arrays.toString(result));
}
}
Output
result = [a, e, f]
Here, to split a string at +, we have used \\+. It's because + is a special character (has a special meaning in regular expressions).