Java String split() Method with examples

The Java String split method is extremely useful for dividing a string into smaller substrings based on some delimiter or regular expression. You can use this method to quickly and easily parse out information.

This article will provide examples of how to write a String split function. The syntax for the String split function in Java Programming is shown below.

Post On:split() Method Of String in Java
Post Type:Java Tutorials
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:SoftwareTestingo Telegram Group

Java String split() Syntax

This Java function can be used to split a string into an array of substrings based on the separator. The parameter for this function can be a regular expression, which allows you to specify how the original string should be divided up. This is useful if you break apart a string with multiple instances of the same character or substring.

We have two split() method versions in the String class.

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

Now, let us try to understand both ways with some simple examples so that we can easily understand the use of the split() method in a Java program.

public String[] split(String regex)

The Java string split() method splits the given string based on a regular expression. The returned array will include the original string, but this split() method Splits the original string into substrings based on the regular expression provided. This function was introduced in Java 1.4, and any trailing empty strings are not included as part of the output array.

Syntax:

public String[] split(String regex); 

//In order to use in program
String_Object.split(String regex);

This Java split function accepts one argument or a regular expression that will be used as the separator.

  • Parameter (regex) – The delimiting regular expression
  • Returns: The split() method will return an array of strings computed by splitting the given string.
  • PatternSyntaxException Exception: If you see a PatternSyntaxException, it means that there is something wrong with the regular expression you are using. Check to make sure that your syntax is correct.
Program: Understand When you get PatternSyntaxException.
package com.SoftwareTestingO.Java.basics;
public class SplitPatternSyntaxExceptionEx 
{
	public static void main(String[] args) 
	{
		String str="Welcome to SoftwareTestingo Blog";
		String arr[]=str.split("[");
		for (String a : arr)
			System.out.println(a);
	}
}

NullPointerException: If you pass null in the split() method, you will get NullPointerException.

Program: Understand When you get NullPointerException.
package com.SoftwareTestingO.Java.basics;
public class SplitPatternSyntaxExceptionEx2 
{
	public static void main(String[] args) 
	{
		String str="Welcome to SoftwareTestingo Blog";
		String arr[]=str.split(null);
		for (String a : arr)
			System.out.println(a);
	}
}

Using Delimiter: If you want to split a string based on a given token. Then you can check the program below where we have split a string with the delimiter colon”:”.

Program: Write a Java Program to split a string based on :.
package com.softwaretestingo.string;
public class StringSplitEx3 
{
	public static void main(String[] args) 
	{
		String str="Learning:Welcome to SoftwareTestingo Blog";
		String arr[]=str.split(":");
		for (String a : arr)
			System.out.println(a);
	}
}

Output:

Learning
Welcome to SoftwareTestingo Blog

With White Space: We can also split the string based on the white spaces. For this, we can use ” ” or “\\s”

Program: Write a Java program to split a string using the delimiter spaces ” “.
package com.softwaretestingo.string;
public class StringSplitEx4 
{
	public static void main(String[] args) 
	{
		String str="Welcome to SoftwareTestingo Blog";
		String arr[]=str.split(" ");
		for (String a : arr)
			System.out.println(a);
	}
}

Output:

Welcome
to
SoftwareTestingo
Blog
Program: Write a Java program to split a string using the regular expression delimiter.
package com.SoftwareTestingO.Java.basics;
public class SplitPatternSyntaxExceptionEx4 
{
	public static void main(String[] args) 
	{
		String str="Welcome to SoftwareTestingo Blog";
		String arr[]=str.split("\\s");
		for (String a : arr)
			System.out.println(a);
	}
}

Output:

Learn
Welcome
to
SoftwareTestingo
Blog

Split String by Multiple Delimiters: This Java program demonstrates how to split a string with multiple delimiters using the OR (|) operator. The example uses two delimiters, white space, and a dot.

Program: Write a Java program to split a string using the multiple delimiter regular expression.
package com.softwaretestingo.string;
public class StringSplitEx6 
{
	public static void main(String[] args) 
	{
		String str="Learn.Welcome to SoftwareTestingo Blog";
		String arr[]=str.split("\\.|\\s");
		for (String a : arr)
			System.out.println(a);
	}
}

Output:

Learn
Welcome
to
SoftwareTestingo
Blog
Interview Program: Suppose there is a string “Hi User Welcome to SoftwareTestingo Blog “. Find the Number of Words in this string.
package com.softwaretestingo.string;
public class StringSplitEx7 
{
	public static void main(String[] args) 
	{
		String str="Hi User Welcome    to    SoftwareTestingo Blog    ";
		String arr[]=str.split("\\s+");
		System.out.println("The Total Number of Words Is: "+arr.length);
		for (String a : arr)
			System.out.println(a);
	}
}

Output:

The Total Number of Words Is: 6
Hi
User
Welcome
to
SoftwareTestingo
Blog

If you are using \\s, that means it matches a single whitespace character, and \\s+ means it matches a sequence of one or more whitespace characters.

Using String: We can split by using String.

Program: Write a Java program to split a string using a word.
package com.softwaretestingo.string;
public class StringSplitEx8 
{
	public static void main(String[] args) 
	{
		String str="Welcome to SoftwareTestingo Blog";
		String arr[]=str.split("to");
		for (String a : arr)
			System.out.println(a);
	}
}

Output:

Welcome 
 SoftwareTestingo Blog

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

This Java String split method is used when we want the substrings to be limited. The only difference between this and other methods is that it limits the number of strings returned after a string is split.

Parameter (regex, limit) – The delimiting regular expression(regex) &  the maximum number of tokens threshold.

For the limit parameter, we can have three values:

  • Limit> 0 means that the pattern will be applied a maximum of limit-1 times, the resulting array will be no longer than n, and the final element in the array will contain everything after the last matching pattern.
  • Limit < 0 – The pattern will be applied as often as possible, and the resulting array can be any size.
  • Limit = 0 – The pattern will be applied as often as possible until it can’t be applied anymore. The resulting array can be of any size, and trailing empty strings will be discarded.

Returns: The split() method will return an array of strings computed by splitting the given string.

Example 1:

package com.softwaretestingo.string;
public class StringSplitEx8 
{
	public static void main(String[] args) 
	{
		String str="Welcome@to@SoftwareTestingo@Blog ";
		String arr[]=str.split("@", 2);
		for (String a : arr)
			System.out.println(a);
	}
}

Output:

Welcome
to@SoftwareTestingo@Blog 

If we pass 3 in the method, then the output will be
String arr[]=str.split(“@”, 3);

Welcome
to
SoftwareTestingo@Blog 

Example 2:

package com.softwaretestingo.string;
public class StringSplitEx10 
{
	public static void main(String[] args) 
	{
		String str="Welcome@to@SoftwareTestingo@Blog ";
		String arr[]=str.split("@", -2);
		for (String a : arr)
			System.out.println(a);
	}
}

Output:

Welcome
to
SoftwareTestingo
Blog 

Example 3:

package com.softwaretestingo.string;
public class StringSplitEx11 
{
	public static void main(String[] args) 
	{
		String str="Welcome@to@SoftwareTestingo@Blog ";
		String arr[]=str.split("@", 0);
		for (String a : arr)
			System.out.println(a);
	}
}

Output:

Welcome
to
SoftwareTestingo
Blog

Conclusion:

The Java String class’ split() method is a very useful tool that’s often used. Most data, especially from reading files, requires pre-processing, such as splitting the string, to get meaningful information.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for 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 softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment