Splitting strings in programming is a common practice that allows programmers to break a string according to some specific character or substring. It helps them to derive some crucial information from these strings or substrings.
However, if you want to learn how to split a string in Java, you have landed on the right webpage. This Java article has all the possible methods to split a string input with the respective code snippets.
Method 1: Splitting the string using the split() method
The split() is a method in Java that breaks the Java string into a char array. It breaks the Java string against a given regular expression and has two variants.
Syntax
string.split(String Regex, int limit)
The parameters of the split() method are:
The split() method has two parameters, regex, and limit.
- regex: The method splits the string at this Regex (which accepts only string inputs)
- limit: This parameter controls the number of resulting substrings and is optional.
Note: If users do not pass anything in the limit parameter, the split() method returns all possible substrings.
Example:
class Demo {
public static void main(String[] args) {
String a = "Here we consider a simple example of Java split() method";
String[] res = a.split("");
System.out.print("The output = ");
for (String b : res) {
System.out.print(b + ", ");
}
}
}
Output:
Explanation:
Here, the split() method divided the string input into an array of substrings. It breaks each word with a comma, as given in the regular expression.
The Java split() method without limit parameter
Code Snippet
import Java.util.Arrays;
class Demo {
public static void main(String[] args) {
String a = "This is-a String";
String[] splitting_String = a.split("-");
System.out.println("The output = " + Arrays.toString(splitting_String));
}
}
Output:
Explanation:
Here, in the above example, we split the string at -. Since we have not passed the limit parameter, the method returns an array containing all the substrings. It is an example of a simple character/substring instead of a typical regular expression.
Once the method splits the string, it returns the output as an array of substrings. The result appears in the same order as in the original Java string.
Method 2: The Java split() method with the limit parameter
The limit parameter can accept one of three forms, i.e., less than or greater than an integer or above zero. There are two conditions based on which the method returns the array of substrings:
- When the limit parameter is zero or negative, split() returns an array having all substrings.
- When the limit parameter is positive (suppose n), split() returns an array having n substrings (maximum).
Let us dig deeper into how these three limits work:
- A positive limit indicates the split() method will break the string to a maximum of limit - 1 time. Except this, the method will return the rest of the Java string as the last element of the array. It will not split the string further. The length of the output array will always be less than or the same as that of the limit.
- A negative limit indicates the split() method will break the string at the delimiter multiple times as long as possible. It will bypass overlooking the specific negative value set.
- When users set the limit to zero, it indicates the split() method will break the string at the delimiter multiple times as long as possible. It will again avoid the limit on the length of the output array.
Code Snippet
import Java.util.Arrays;
class Demo {
public static void main(String[] args) {
String a = "ab:cd:ef:gh:hi:j";
// limit is -3; array has all substrings
String[] res = a.split(":", -3);
System.out.println("The output is when limit is -3 = " + Arrays.toString(res));
// limit is zero; array has all substrings
res = a.split(":", 0);
System.out.println("The output is when limit is 0 = " + Arrays.toString(res));
// limit is 1; the method returns a maximum of an array of one substring
res = a.split(":", 1);
System.out.println("The output is when limit is 2 = " + Arrays.toString(res));
// limit is 5; array has a maximum of five substrings
res = a.split(":", 5);
System.out.println("The output is when limit is 5 = " + Arrays.toString(res));
}
}
Output:
Explanation:
As we can see, the above example has returned different outputs. A limit having a negative value returns an array of substrings with multiple substrings.
The limit parameter with a positive value will return an array of substrings at most three (limit - 1) times. And the limit with zero as the value will return an array having all the substrings.
Some Important Points about the Special Characters:
- Of course, in all the above examples, we have used special characters. These are Regex (regular expressions) that users must remember and escape when they want their literal value. Users can use the Regex parameter as the delimiter in the split() method, which is a regular expression. But using this, users must ensure to escape special characters if they want to apply the literal value as a delimiter.
- There are twelve such characters in the regular expression. These are: \, ^, $, ., |, ?, *, +, (, ), [, {. Users must take care of escaping these characters while using the split techniques to a Java string at one of these characters. Users can do this by using the backslash \.
Example:
string.split("\\|");
- The backlash will split the string at the | character. Here, we have used two backlashes because we first have to escape the Java meaning of the backlash. Then, the second backlash will split the | character.
- An alternative and more efficient way to do this is using the set []. It separately indicates all the special characters users want to escape by putting them inside square brackets. This method will treat all the special characters as Java normal characters.
Example:
string.split("[|]");
Conclusion
We hope the article has discussed all the methods for splitting a Java string. The methods are the split() method with and without limit parameters, where both these methods are interconnected but have special significance.
We have highlighted some points about some special characters of a regular expression used as a parameter (regex) in the split() method.