The split(CharSequence, int) method of the Pattern class is used to split an input character sequence around matches of a regular expression. It returns an array of substrings, where each element is separated by the matched pattern. The limit parameter controls how many times the pattern is applied and affects the size of the resulting array.
- Supports regular expression delimiters.
- Preserves trailing values based on limit.
- Accepts any CharSequence as input.
import java.util.regex.*;
public class Geeks {
public static void main(String[] args) {
Pattern pattern = Pattern.compile(",");
String text = "Alice,Bob,Charlie,David";
int limit = 2;
String[] result = pattern.split(text, limit);
for (int i = 0; i < result.length; i++) {
System.out.println("result[" + i + "] = " + result[i]);
}
}
}
Output
result[0] = Alice result[1] = Bob,Charlie,David
Explanation: The string is split using the comma (",") as the delimiter. Since the limit is 2, only one split is performed, producing two substrings.
Syntax
public String[] split(CharSequence input, int limit)
Parameters: This method accepts two parameter one
- input: which represents character sequence to be split and other
- limit: which represents The result threshold as mentioned in description.
Return value: This method returns the array of strings computed by splitting the input around matches of this pattern. Below programs illustrate the split(CharSequence, int) method.
How the limit Parameter Works
| Limit Value | Behavior |
|---|---|
| limit > 0 | The pattern is applied at most limit - 1 times. |
| limit = 0 | The pattern is applied as many times as possible, and trailing empty strings are removed. |
| limit < 0 | The pattern is applied as many times as possible, and trailing empty strings are preserved. |
Example: Split Using Comma with limit = 3
import java.util.regex.Pattern;
public class GFG {
public static void main(String[] args) {
Pattern pattern = Pattern.compile(",");
CharSequence input = "Java,Python,C++,JavaScript";
String[] result = pattern.split(input, 3);
for (String str : result) {
System.out.println(str);
}
}
}
Output
Java Python C++,JavaScript
Explanation: The string is split using the comma (,) as the delimiter. Since the limit is 3, only two splits are performed. The first two elements contain "Java" and "Python", while the remaining text is stored in the last element.
Example 2: Split Using Hyphen with limit = 2
import java.util.regex.Pattern;
public class GFG {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("-");
CharSequence input = "2026-07-09-Thursday";
String[] result = pattern.split(input, 2);
for (String str : result) {
System.out.println(str);
}
}
}
Output
2026 07-09-Thursday
Explanation: The pattern "-" is used as the delimiter. Since the limit is 2, the string is split only once. The first element contains "2026", while the remaining date is stored in the second element.
Example: Split Using Multiple Spaces with limit = 0
import java.util.regex.Pattern;
public class GFG {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\s+");
CharSequence input = "Java Python C++ JavaScript";
String[] result = pattern.split(input, 0);
for (String str : result) {
System.out.println(str);
}
}
}
Output
Java Python C++ JavaScript
Explanation: The regular expression \\s+ matches one or more whitespace characters. Since the limit is 0, the pattern is applied as many times as possible, splitting the string wherever one or more spaces occur. Any trailing empty strings are removed.
Advantages of split(CharSequence, int) Method
- Splits strings using regular expressions instead of fixed characters.
- Provides control over the number of splits using the limit parameter.
- Supports complex delimiters through regular expressions.
- Returns the split values as a String array for easy processing.
- Useful for parsing structured text such as CSV data, log files, and formatted strings.