String split()

This method returns an array of string by splitting the given string into multiple words based on the specified delimiter or regular expression.

Sometime, in our application we may need to divide or split the string based on some special characters like “|” or empty space and in all such cases we can use split() method.

There are 2 overloaded methods for split()


public String[] split(String regex)

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


Let’s discuss each one of them with example


public String[] split(String regex)

This method splits the given string based on the regular expression passed to this method.

All the tokens or words after split will be returned as array of String by this method.

Example

1
2
3
4
5
6
7
8
9
10
public class StringSplitExample1{  
public static void main(String args[]){  
String str="java|in|simple|way";  
String[] tokens=str.split("\\|");//splits the string based on pipe symbol  
//loop the returned array to print each element of string array  
for(String token:tokens){  
System.out.println(token);  
}  
}
}  
public class StringSplitExample1{  
public static void main(String args[]){  
String str="java|in|simple|way";  
String[] tokens=str.split("\\|");//splits the string based on pipe symbol  
//loop the returned array to print each element of string array  
for(String token:tokens){  
System.out.println(token);  
}  
}
}  



We can see that string is split based on pipe symbol and returned a new array of string


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

This method splits the given string based on the regular expression passed to this method but It takes one extra parameter called “limit”.

The limit parameter controls the number of strings returned after split.

So resulting array of string is not just based on split by matching regular expression but also depends on the limit specified.

If we specify the limit as 4 then split method returns an array of only 4 strings even through there is regular expression matching more than 4 times in the string.

If limit is negative, returned array of string will have as many substrings as possible based on the regular expression match.

If limit is zero, returned array of string will have as many substrings as possible based on the regular expression match but trailing space will be discarded.

It throws PatternSyntaxException if the syntax of specified regular expression is invalid.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class StringSplitExample2{
public static void main(String args[]){
String str="java|in|simple|way";  
System.out.println("split(String regex, int limit) with limit=0:");
for(String token:str.split("\\|",0)){
System.out.println(token);
}
System.out.println();
System.out.println("split(String regex, int limit) with limit=1:");
for(String token:str.split("\\|",1)){
System.out.println(token);
}
System.out.println();
System.out.println("split(String regex, int limit) with limit=2:");
for(String token:str.split("\\|",2)){
System.out.println(token);
}
System.out.println();
System.out.println("split(String regex, int limit) with limit=-1:");
for(String token:str.split("\\|",-1)){
System.out.println(token);
}
 
}
}
public class StringSplitExample2{
public static void main(String args[]){
String str="java|in|simple|way";  
System.out.println("split(String regex, int limit) with limit=0:");
for(String token:str.split("\\|",0)){
System.out.println(token);
}
System.out.println();
System.out.println("split(String regex, int limit) with limit=1:");
for(String token:str.split("\\|",1)){
System.out.println(token);
}
System.out.println();
System.out.println("split(String regex, int limit) with limit=2:");
for(String token:str.split("\\|",2)){
System.out.println(token);
}
System.out.println();
System.out.println("split(String regex, int limit) with limit=-1:");
for(String token:str.split("\\|",-1)){
System.out.println(token);
}

}
}



We can see that string is split based on pipe symbol and also the limit specified.

In the above example split with limit 0 and -1 returned same value however in some cases the result would be different.


Let’s see with the help of below example:

1
2
3
String s=" boo:and:foo";
String res1[]= s.split("o", -1);
String res2[]= s.split("0", 0);
String s=" boo:and:foo";
String res1[]= s.split("o", -1);
String res2[]= s.split("0", 0);


In this case arr1 would be having { “b”, “”, “:and:f”, “”, “” } However arr2 would be having { “b”, “”, “:and:f” } because limit zero excludes trailing empty spaces.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on