English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Java String split() method splits the string at the specified regular expression and returns an array of substrings.
The syntax of the string split() method is:
The string.split(String regex, int limit) method.
The split() method of the string can take two parameters:
regex - The string is split at this regular expression (which can be a string)
limit (Optional)-Specify the number of generated substrings
If no parameter limit is passed, split() returns all possible substrings.
Returns an array of substrings.
Note:If the passed regular expression split() is invalid, the split() method throws a PatternSyntaxException.
//Import the array to convert the array to a string //Used to print the array import java.util.Arrays; class Main { public static void main(String[] args) { String vowels = "a::b::c::d:e"; //Split the string at "::" //Store the result in a string array String[] result = vowels.split("::"); //Convert the array to a string and print System.out.println("result = ", + Arrays.toString(result)); } }
Output Result
result = [a, b, c, d:e]
Here, we split the string at ::. Since no parameter limit is passed, the returned array contains all substrings.
If the limit parameter is 0 or negative, split() returns an array containing all substrings.
If the limit parameter is positive (e.g., n), split() returns the maximum number of substrings n.
//Import an array, convert the array to a string import java.util.Arrays; class Main { public static void main(String[] args) { String vowels = "a:bc:de:fg:h"; // Split the string at ":" // limit = -2; The array contains all substrings String[] result = vowels.split(":", -2); System.out.println("returns when limit is -2 returns = " + Arrays.toString(result)); // limit = 0; The array contains all substrings result = vowels.split(":", 0); System.out.println("returns when limit is 0 = " + Arrays.toString(result)); // limit = 2; The array can contain at most2number of substrings result = vowels.split(":", 2); System.out.println("returns when limit is 2 returns = " + Arrays.toString(result)); // limit = 4; The array can contain at most4number of substrings result = vowels.split(":", 4); System.out.println("returns when limit is 4 returns = " + Arrays.toString(result)); // limit = 10; The array can contain at most10number of substrings result = vowels.split(":", 10); System.out.println("returns when limit is 10 returns = " + Arrays.toString(result)); } }
Output Result
returns when limit is -2 returns = [a, bc, de, fg, h] returns when limit is 0 = [a, bc, de, fg, h] returns when limit is 2 returns = [a, bc:de:fg:h] returns when limit is 4 returns = [a, bc:de:fg:h] returns when limit is 10 returns = [a, bc, de, fg, h]
Note: split()The method takes a regular expression as the first parameter. If special characters, such as \, |, ^, are needed,*、+ such as, you need to escape these characters. For example, we need to use \\+ to split +.
//Import the array to convert the array to a string //Used to print the array import java.util.Arrays; class Main { public static void main(String[] args) { String vowels = "a+e+f"; //Split the string at+ String[] result = vowels.split("\\+"); //Convert the array to a string and print System.out.println("result = ", + Arrays.toString(result)); } }
Output Result
result = [a, e, f]
Here, to be+split the string at the position, we used \\+. This is because+is a special character (with special meaning in regular expressions).