Java – String split() method

In this article, we will discuss how to split strings using specified delimiter in String’s split() method

1. String’s split() method:

  • This String method is used to split strings using specified delimiter (i.e.; using regex pattern) and
  • returns string[] array

Note:

  • there are 2 variants or overloaded split() methods
  • 1st variant by default split string using specified delimiter
  • Whereas 2nd variant split string using specified delimiter but with threshold limit specified, as other argument (2nd argument)

1.1 Method Signature:

public String[] split(String regex);

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

1.2 Parameters:

  • regex   –> regular expression which is used as delimiter to split string
  • limit    –> in the 2nd variant, we can specify threshold limit i.e.; in how many maximum parts string should be split-ed

1.3 Returns:

split() method

Returns

public String[] split(String regex);Returns string[] arrays after splitting the invoking string using specified delimiter (or regex)
public String[] split(String regex, int limit);Returns string[] arrays after splitting the invoking string using specified delimiter (or regex) with specified threshold limit

1.4 Throws:

  • PatternSyntaxException, if the specified regular expression syntax is invalid (or simply not compliable)

2. Examples on split() method:

2.1 Java program to split strings (with 3 different possible delimiter)

The delimiter are,

  • space (” “)
  • Dot or period (“.”)
  • forward slash (“/”)

Method Signature:

public String[] split(String regex);

StringSplitMethod.java

package in.bench.resources.string.methods;

public class StringSplitMethod {

	public static void main(String[] args) {

		// Example 1: test string
		String testStr1 = "English is a easy language "
				+ "but Java is super cool language";

		// split using space (" ")
		String[] splittedStrArrays1 = testStr1.split(" ");

		// print to console
		System.out.println("Example 1 : split(\" \") "
				+ "using space\n");
		System.out.println("Original string : " + testStr1);
		System.out.println("\nSplitted string : ");

		// iterating using enhanced for-loop
		for(String str1 : splittedStrArrays1) {
			System.out.println(str1);
		}
		
		

		// Example 2: test string
		String testStr2 = "www.benchresources.net";

		// split using DOT or period (".")
		String[] splittedStrArrays2 = testStr2.split("\\.");

		// print to console
		System.out.println("\n\nExample 2 : split(\".\") "
				+ "using DOT or period\n");
		System.out.println("Original string : " + testStr2);
		System.out.println("\nSplitted string : ");

		// iterating using enhanced for-loop
		for(String str2 : splittedStrArrays2) {
			System.out.println(str2);
		}
		
		

		// Example 3: test string
		String testStr3 = "30/07/2017";

		// split using DOT or period (".")
		String[] splittedStrArrays3 = testStr3.split("/");

		// print to console
		System.out.println("\n\nExample 3 : split(\"/\") "
				+ "using forward-slash\n");
		System.out.println("Original string : " + testStr3);
		System.out.println("\nSplitted string : ");

		// iterating using enhanced for-loop
		for(String str3 : splittedStrArrays3) {
			System.out.println(str3);
		}
	}
}

Output:

Example 1 : split(" ") using space

Original string : English is a easy language but Java is super cool language

Splitted string : 
English
is
a
easy
language
but
Java
is
super
cool
language


Example 2 : split(".") using DOT or period

Original string : www.benchresources.net

Splitted string : 
www
benchresources
net


Example 3 : split("/") using forward-slash

Original string : 30/07/2017

Splitted string : 
30
07
2017

2.2 Java program to split string with specified threshold limit (with 3 different possible delimiter)

The delimiter are,

  • space (” “)
  • Dot or period (“.”)
  • forward slash (“/”)

Method Signature:

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

StringSplitMethod2.java

package in.bench.resources.string.methods;

public class StringSplitMethod2 {

	public static void main(String[] args) {

		// Example 1: test string
		String testStr1 = "English is a easy language "
				+ "but Java is super cool language";

		// split using space (" ") - with threshold-limit 7
		String[] splittedStrArrays1 = testStr1.split("\\s", 7);

		// print to console
		System.out.println("Example 1 : split(\" \") using space"
				+ " - with threshold-limit 7\n");
		System.out.println("Original string : " + testStr1);
		System.out.println("\nSplitted string : ");

		// iterating using enhanced for-loop
		for(String str1 : splittedStrArrays1) {
			System.out.println(str1);
		}



		// Example 2: test string
		String testStr2 = "www.benchresources.net";

		// split using DOT or period (".")
		// with threshold-limit 2
		String[] splittedStrArrays2 = testStr2.split("\\.", 2);

		// print to console
		System.out.println("\n\nExample 2 : split(\".\") "
				+ "using DOT or period"
				+ " - with threshold-limit 2\n");
		System.out.println("Original string : " + testStr2);
		System.out.println("\nSplitted string : ");

		// iterating using enhanced for-loop
		for(String str2 : splittedStrArrays2) {
			System.out.println(str2);
		}



		// Example 3: test string
		String testStr3 = "30/07/2017";

		// split using DOT or period (".")
		// with threshold-limit 2
		String[] splittedStrArrays3 = testStr3.split("/", 2);

		// print to console
		System.out.println("\n\nExample 3 : split(\"/\") "
				+ "using forward-slash"
				+ " - with threshold-limit 2\n");
		System.out.println("Original string : " + testStr3);
		System.out.println("\nSplitted string : ");

		// iterating using enhanced for-loop
		for(String str3 : splittedStrArrays3) {
			System.out.println(str3);
		}
	}
}

Output:

Example 1 : split(" ") using space - with threshold-limit 7

Original string : English is a easy language but Java is super cool language

Splitted string : 
English
is
a
easy
language
but
Java is super cool language


Example 2 : split(".") using DOT or period - with threshold-limit 2

Original string : www.benchresources.net

Splitted string : 
www
benchresources.net


Example 3 : split("/") using forward-slash - with threshold-limit 2

Original string : 30/07/2017

Splitted string : 
30
07/2017

Hope, you found this article very helpful. If you have any suggestions or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String startsWith() method
Java - String replace() method