split() method in Java is used to divide a string into an array of substrings based on a specified delimiter or regular expression. The result of the split operation is always a String[]. This method is widely used for parsing input, processing text, and handling structured string data.
Example: Split a String by Single Space
public class Geeks {
public static void main(String[] args) {
String s = "Geeks for Geeks";
String[] arr = s.split(" ");
for (String str : arr) {
System.out.println(str);
}
}
}
Output
Geeks for Geeks
To know more about regex, please refer to the article: Java Regular Expressions
Syntax of split() Method
public String[] split(String regex, int limit)
Parameters:
- regex: Regular expression used as the delimiter.
- limit (Optional): Controls the number of resulting substrings.
Return Type: "String[]" An array of strings is computed by splitting the given string.
Exception: "PatternSyntaxException" if the provided regular expression’s syntax is invalid.
Understanding limit in split(regex, limit)
Limit Value | Behavior |
|---|---|
limit > 0 | Splits at most limit - 1 times |
limit = 0 | Splits fully, removes trailing empty strings |
limit < 0 | Splits fully, keeps trailing empty strings |
Example 1: Split Using Multiple Delimiters (Regex)
This code depicts how a string can be split using spaces, commas, and dots together.
public class Geeks {
public static void main(String[] args) {
String s = "This is,comma.fullstop whitespace";
String regex = "[,\\s\\.]";
String[] arr = s.split(regex);
for (String str : arr) {
System.out.println(str);
}
}
}
Output
This is comma fullstop whitespace
Explanation: regex = "[,\\s\\.]" matches commas, spaces, and dots. The string is split wherever any of these characters appear.
Example 2: split(regex, limit) with Small Limit
The program explains how a positive limit restricts the number of splits.
public class Geeks {
public static void main(String args[]) {
String s = "geeks@for@geeks";
String[] arr = s.split("@", 2);
for (String a : arr)
System.out.println(a);
}
}
Output
geeks for@geeks
Explanation: The string is split only once (limit - 1) and the remaining content is stored in the last element.
Example 3: split(regex, limit) with Negative Limit
The code depicts how a negative limit allows unlimited splitting.
public class Geeks {
public static void main(String args[]) {
String s = "geeks@for@geeks";
String[] arr = s.split("@", -2);
for (String a : arr)
System.out.println(a);
}
}
Output
geeks for geeks
Explanation: Negative limit performs all possible splits. No elements gets discarded.
Example 4: Split by a Specific Word
Following example splits a string using a substring instead of a character.
public class Geeks {
public static void main(String args[]) {
String s = "GeeksforGeeksforStudents";
String[] arr = s.split("for");
for (String a : arr)
System.out.println(a);
}
}
Output
Geeks Geeks Students
Explanation: The delimiter "for" appears twice. Each occurrence causes a split.
Example 5: Split Using Dot (.)
The example highlights why special regex characters must be escaped.
public class Geeks {
public static void main(String args[]) {
String s = "Geeks.for.Geeks";
String[] arr = s.split("[.]");
for (String a : arr)
System.out.println(a);
}
}
Output
Geeks for Geeks
Explanation: . is a regex wildcard and must be escaped. [.] ensures splitting only on literal dots.
Example 6: Split Using Complex Regular Expression
public class Geeks {
public static void main(String args[]) {
String s = "w1, w2@w3?w4.w5";
String[] arr = s.split("[, ?.@]+");
for (String a : arr)
System.out.println(a);
}
}
Output
w1 w2 w3 w4 w5
Explanation: The regex splits the string at any listed symbol. + ensures consecutive delimiters are treated as one.
Example 7: Delimiter Not Present in String
public class Geeks {
public static void main(String args[]) {
String s = "GeeksforGeeks";
String[] arr = s.split("#");
for (String a : arr)
System.out.println(a);
}
}
Output
GeeksforGeeks
Explanation: Since "#" is not found, the original string remains unchanged. The array contains only one element.