String split() method in java

In this post, we will read what is the string spilt() method(split method in java) and how to use the string spilt in java. In a recent post, we have read many things about the string in java. Let’s see how to use the string split() method.

Here is the table content of the article will we will cover this topic.
1. What is the string spilt() in Java?
2. String split(String regex)
3. String split(String regex, int limit)

split method in java

What is the string spilt() in Java?

The String spilt() method in java used to split the string into parts and return the Array of characters. As we know string is a group of characters or an array of characters, the split() method can break the string in an array of characters. By use of this method, we break the string-based of particular delimiter. The String spilt in java can be based on space or a comma(,) or it depends on requirement.

It is an overloaded method in the String class. It has two types:
1. String split(String regex)
2. String split(String regex, int limit)

1. String split(String regex)

This method is defined in the String class and it takes only one parameter. Based on the regex parameter, The method breaks the current string and returns String array.

stringName.split(regex);

stringName: It is the current string that you want to split in different chunks based on regex.
regex: It is a String type parameter. It is the condition which used to split the string.

class ExampleOfSplit
 {  
		 public static void main(String args[])
		 {  
			String str1 = "JAVA GOAL SITE";  
			String[] nameArray1 = str1.split(" ");
			System.out.println("Split by white space:");
			for(String name: nameArray1)
				System.out.println(name);
			
			String str2 = "JAVA,GOAL,SITE";  
			String[] nameArray2 = str2.split(",");
			System.out.println("Split by Comma:");
			for(String name: nameArray2)
				System.out.println(name);
			
			String str3 = "JAVAGOALSITE";  
			String[] nameArray3 = str3.split("");
			System.out.println("Split each character:");
			for(String name1: nameArray3)
				System.out.println("Split by comma = "+name1);
	    }  
}  

Output: Split by white space:
JAVA
GOAL
SITE
Split by Comma:
JAVA
GOAL
SITE
Split each character:
Split by comma = J
Split by comma = A
Split by comma = V
Split by comma = A
Split by comma = G
Split by comma = O
Split by comma = A
Split by comma = L
Split by comma = S
Split by comma = I
Split by comma = T
Split by comma = E

2. String split(String regex, int limit)

This method returns String array on based the given regex. It takes only two parameters. One is the regex, and another is the limit. By use of the second parameter(limit), you can define the maximum limit of chunks.

stringName.split(regex, limit);

stringName: It is the current string that you want to split in different chunks based on regex.
regex: It is a String type parameter. It is the condition which used to split the string.
limit: The number of parts in which you want to divide the string. If we don’t provide or zero, it will return all the strings matching a regex.

class ExampleOfSplit
 {  
		 public static void main(String args[])
		 {  
			String str1 = "JAVA GOAL SITE";  
			String[] nameArray1 = str1.split(" ", 2);
			System.out.println("Split by white space:");
			for(String name: nameArray1)
				System.out.println(name);
			
			String str2 = "JAVA,GOAL,SITE";  
			String[] nameArray2 = str2.split(",", 1);
			System.out.println("Split by Comma:");
			for(String name: nameArray2)
				System.out.println(name);
			
			String str3 = "JAVAGOALSITE";  
			String[] nameArray3 = str3.split("", 8);
			System.out.println("Split each character:");
			for(String name1: nameArray3)
				System.out.println("Split by comma = "+name1);
		 }  
}  

Output: Split by white space:
JAVA
GOAL SITE
Split by Comma:
JAVA,GOAL,SITE
Split each character:
Split by comma = J
Split by comma = A
Split by comma = V
Split by comma = A
Split by comma = G
Split by comma = O
Split by comma = A
Split by comma = LSITE

If the limit n > 0, the array’s length can’t be greater than n, and the last entry of array contains all input beyond the last matched delimiter.

If n < 0, The array can have any length.

If n > 0, The array can have any length, and trailing empty strings will be discarded.

class ExampleOfSplit
 {  
		 public static void main(String args[])
		 {  
			String str1 = "JAVA GOAL SITE";  
			// limit is -2; array contains all substrings
	        String[] result = str1.split("", -2);
	        System.out.println("Array when limit is -2:");
	        for(String s : result)
	        	System.out.println(s);

	        // limit is 0; array contains all substrings
	        result = str1.split("", 0);
	        System.out.println("Array when limit is 0:"); 
	        for(String s : result)
	        	System.out.println(s);
	        
	        // limit is 2; array contains a maximum of 2 substrings
	        result = str1.split("", 2);
	        System.out.println("Array when limit is 2:");
	        for(String s : result)
	        	System.out.println(s);
	        
	        // limit is 4; array contains a maximum of 4 substrings
	        result = str1.split("", 4);
	        System.out.println("Array when limit is 4:");
	        for(String s : result)
	        	System.out.println(s);
	        
	        // limit is 10; array contains a maximum of 10 substrings
	        result = str1.split("", 10);
	        System.out.println("Array when limit is 10:");
	        for(String s : result)
	        	System.out.println(s);
			
		 }  
}  

Output: Array when limit is -2:
J
A
V
A

G
O
A
L

S
I
T
E

Array when limit is 0:
J
A
V
A

G
O
A
L

S
I
T
E
Array when limit is 2:
J
AVA GOAL SITE
Array when limit is 4:
J
A
V
A GOAL SITE
Array when limit is 10:
J
A
V
A

G
O
A
L
SITE

Leave a Comment