The split() method in Java is used to divide a string into multiple parts based on a specified delimiter (regular expression). It returns an array of substrings after splitting the original string. This method is commonly used for parsing data like CSV values, sentences, or formatted input.
- Uses regular expressions (regex) as the delimiter
- Returns an array of strings after splitting
- The limit parameter controls the number of splits and resulting array size
Syntax:
String[] result = string.split(String regex);
Or
String[] result = string.split(String regex, int limit);
Parameters:
- regex: Regular expression used as the delimiter.
- limit: Controls the number of resulting substrings.
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
Explanation: In this example, a string "Geeks for Geeks" is created and split using the split(" ") method, where a space is used as the delimiter. This divides the string into individual words and stores them in an array arr. A for-each loop then iterates over the array and prints each word on a new line.
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 |
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.
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.
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.
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.
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.
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.
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.