Java String split() Method
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
The split() method in Java splits a String into an array of substrings based on a delimiter. It is beneficial for breaking up Strings into parts for easier processing.
The split() method accepts a regular expression as a parameter and splits the string around matches of the regular expression. The resultant substrings are returned in an array.
For example, splitting a string on whitespace can be done like:
String str = "Hello World";
String[] parts = str.split("\\s+"); // split on whitespaceThis would result in a String array with two elements, “Hello” and “World”.
Variants of the split() Method in Java
There are two main variants of split() in Java:
Variant 1:
public String[] split(String regex, int limit)
This variant allows for the specification of a limit on the number of splits performed.
Parameters:
- regex – A delimiting regular expression
- limit – The limit parameter controls the number of times the pattern is applied and, therefore, affects the length of the resulting array.
Returns:
An array of strings is computed by splitting the given string around matches of the given regular expression.
Exception Thrown:
- PatternSyntaxException – If the syntax of the regular expression is invalid
The limit parameter can take three possible values:
- Positive Limit – Splits up to the specified limit as many times as possible. The last element in the array may contain unmatched delimiter characters.
- Zero – Splits all matches of the delimiter. The returned array contains empty strings between contiguous matches of the delimiter.
- Negative Limit – Splits all matches of the delimiter with no limit.
Example:
public class SplitExample {
public static void main(String[] args) {
// Input string
String str = "Hello-World-Java";
// Example 1: Limit of 5
String[] parts1 = str.split("-", 5);
System.out.println("Example 1: Limit of 5");
for (String part : parts1) {
System.out.println(part);
}
// Output: ["Hello", "World", "Java"]
// Example 2: Limit of 0 (default)
String[] parts2 = str.split("-");
System.out.println("\nExample 2: Limit of 0 (default)");
for (String part : parts2) {
System.out.println(part);
}
// Output: ["", "Hello", "World", "Java"]
// Example 3: Negative Limit (no limit)
String[] parts3 = str.split("-", -1);
System.out.println("\nExample 3: Negative Limit (no limit)");
for (String part : parts3) {
System.out.println(part);
}
// Output: ["Hello", "World", "Java"]
}
}Output:
Example 1: Limit of 5
Hello
World
Java
Example 2: Limit of 0 (default)
Hello
World
Java
Example 3: Negative Limit (no limit)
Hello
World
Java
Variant 2:
public String[] split(String regex)
This variant only takes the regular expression to split on.
Parameters:
regex – A delimiting regular expression
Returns:
An array of strings is computed by splitting the input string around matches of the given regular expression.
Exception Thrown:
PatternSyntaxException – If the syntax of the regular expression is invalid.
This variant uses a default limit of 0, i.e. it splits all matches of the delimiter.
For example:
public class SplitExample {
public static void main(String[] args) {
// Input string
String str = "Hello-World-Java";
// Splitting the string using "-"
String[] parts = str.split("-");
// Output the resulting array
for (String part : parts) {
System.out.println(part);
}
}
}Output:
Hello
World
Java
Examples of Using split() Method in Java
Example 1: Split with Small Limit
public class SplitExample {
public static void main(String[] args) {
String str = "apple#orange#banana";
String[] fruits = str.split("#", 2);
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}Output:
apple
orange#banana
Example 2: Split with Large Limit
public class SplitExample {
public static void main(String[] args) {
String str = "one-two-three-four-five";
String[] parts = str.split("-", 10);
for (String part : parts) {
System.out.println(part);
}
}
}Output:
one
two
three
four
five
Example 3: Split with Negative Limit
public class SplitExample {
public static void main(String[] args) {
String str = "a,b,c,d,e";
String[] parts = str.split(",", -1);
for (String part : parts) {
System.out.println(part);
}
}
}Output:
a
b
c
d
e
Example 4: Split with Regular Expression
public class SplitExample {
public static void main(String[] args) {
String str = "bread,milk,eggs";
String[] items = str.split(",");
for (String item : items) {
System.out.println(item);
}
}
}Output:
bread
milk
eggs
Example 5: Split with Regex and Negative Limit
public class SplitExample {
public static void main(String[] args) {
String str = "2022-11-02";
String[] parts = str.split("-", -1);
for (String part : parts) {
System.out.println(part);
}
}
}Output:
2022
11
02
Conclusion
In summary, Java’s split() method is a versatile tool for breaking down strings into substrings based on user-defined delimiters or regular expressions. This method encompasses two primary variants: one that allows you to specify a limit on the number of splits and the other that automatically sets the limit to 0 for maximum splitting.
By mastering these variants and exploring the examples provided, you can efficiently handle string manipulation in Java to cater to your specific processing requirements. Whether you need to split a string with a constrained limit, an extensive limit, a negative limit, or utilize regular expressions, the split() method empowers you with flexibility and adaptability in string processing.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

