Java String split()

The String split() method returns an array of split strings after the method splits the given string around matches of a given regular expression containing the delimiters.

The regular expression must be a valid pattern and remember to escape special characters if necessary.

String str = "A-B-C-D";

String[] strArray = str.split("-");	// [A, B, C, D]

1. The String.split() API

1.1. Syntax

The split() method is overloaded and accepts the following parameters.

  • regex – the delimiting regular expression.
  • limit – controls the number of times the pattern is applied and therefore affects the length of the resulting array.
    • If the limit is positive then the pattern will be applied at most limit – 1 times. The result array’s length will be no greater than limit, and the array’s last entry will contain all input beyond the last matched delimiter.
    • If the limit is zero then result array can be of any size. The trailing empty strings will be discarded.
    • If the limit is negative then result array can be of any size.
public String[] split(String regex);

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

1.2. Throws PatternSyntaxException

Watch out that split() throws PatternSyntaxException if the regular expression’s syntax is invalid. In the given example, “[” is an invalid regular expression.

String[] strArray = "hello world".split("[");

Program output.

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0

1.3. ‘null’ is Not Allowed

The method does not accept ‘null’ argument. It will throw NullPointerException in case the method argument is null.

Exception in thread "main" java.lang.NullPointerException
	at java.lang.String.split(
	at com.StringExample.main(

2. Java Programs to Split a String

2.1. Split with Specified Delimiter

The following Java program splits a string based on a given delimiter hyphen "-".

String str = "how to do-in-java-provides-java-tutorials";

String[] strArray = str.split("-");  //[how to do, in, java, provides, java, tutorials]

2.2. Split by Whitespace

The following Java program splits a string by space using the delimiter "\\s". To split by all white space characters (spaces, tabs etc), use the delimiter “\\s+“.

String str = "how to do injava";

String[] strArray = str.split("\\s");  //[how, to, to, injava]

2.3. Split by Comma

Java program to split a string by delimiter comma.

String str = "A,B,C,D";

String[] strArray = str.split(",");  //[A,B,C,D]

2.4. Split by Multiple Delimiters

Java program to split a string with multiple delimiters. Use regex OR operator '|' symbol between multiple delimiters.

In the given example, I am splitting the string with two delimiters, a hyphen and a dot.

String str = "";

String[] strArray = str.split("-|\\."); //[how, to, do, in, java]

3. Split a String into Maximum N tokens

This version of the method also splits the string, but the maximum number of tokens can not exceed limit argument. After the method has found the number of tokens, the remaining unsplitted string is returned as the last token, even if it may contain the delimiters.

Below given is a Java program to split a string by space in such a way the maximum number of tokens can not exceed 5.

String str = "how to do in java provides java tutorials";

String[] strArray = str.split("\\s", 5);

System.out.println(strArray.length);  //5
System.out.println(Arrays.toString(strArray));  //[how, to, do, in, java provides java tutorials]

4. Conclusion

This Java String tutorial taught us to use the syntax and usage of Spring.split() API, with easy-to-follow examples. We learned to split strings using different delimiters such as commas, hyphens, and even multiple delimiters in a String.

Happy Learning !!

Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial