Split() String method in java with examples

In Java, the split() method belongs to a String class. This method is used to split a string into an array of substrings bases on a specified regular expression. In Java there are many methods along with the split() method.

How to use the split() method

public String[] split(String regex)
public String[] split(String regex, int limit)

Parameters

1.regex:

This is the regular expression that defines the delimiter. The method splits the string whenever it encounters this delimiter.

2.limit:

This parameter specifies the maximum number of substrings to return. If it is a zero or negative, the method will apply no limit.

Examples

Example 1

Let us see the example by specifying regex and see the result or substrings.

public class split{
public static void main(String[] args){
String text = "cat, dog, parrot, rabbit";
String[] pets= text.split(",");
for(String pet : pets){
System.out.println(pet);
}
}
}

Output

cat
dog
parrot
rabbit

In this example, ‘split(“,”)’ splits the ‘text’ string into substrings wherever there is a comma. You can also split based on space split(” “).

Example 2

Let us see the example by specifying the regex and see the result or substrings.

public class split{
public static void main(String[] args){
String text = "Hello! This is Codespeedy.";
String[] words = text.split(" ");
for (String word : words){
System.out.println(word);
}
}
}

Output

Hello!
This
is
Codespeedy.

In this example, ‘split(” “)’ splits the ‘text’ string into substrings wherever there is a space.

Additionally, you can specify a limit to produce the number of substrings by the split.

Example 3

Let us see an example by specifying regex and limits and see the substrings.

public class split{
public static void main(String[] args){
String text = "Hello! This is Codespeedy.";
String[] words= text.split(" ",2);
for(String word : words){
System.out.println(word);
}
}
}

Output

Hello!
This is Codespeedy.

In this example, ‘split(” “)’ splits the ‘text’ string into substrings wherever there is a space. And also you can specify limit like in the above example you mentioned limit size as 2, so we can limit the number of terms up to size 2. so we can see that the first substring is divided but the rest is intact in the second substring.

Example 4

In this example we are dividing the string into substrings using delimiter or regular expression.

public class split{
public static void main(String[] args){
String s = "alpha: beta: gamma: delta.";
String[] words=s.split(":",3);
for(String word : words){
System.out.println(word);
}
}
}

Output

alpha
beta
gamma : delta

In the above example, the string is split using delimiter :. And also the limit size is specified as 3 so the first two substrings are divided using delimiter and rest all is intact in the third substring.

Note:

If the delimiter is not found in the string, the method return an array containing the original string as the only element. You can also you this method when you have multiple delimiters.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top