Java String split() : Splitting by One or Multiple Delimiters

In Java, when working with strings, we may encounter situations where we need to split a string into smaller parts using multiple delimiters. The split() method provided by the String class splits the specified string based on a regular expression, making it a versatile tool for handling various delimiters. It returns an array of split strings after the method splits the given string around matches.

For using the multiple delimiters, the regular expression should define a character class that includes all the delimiters we want to split by.

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

String str = "";

String[] strArray1 = str.split("-"); //[how, to,]  - 3 tokens
String[] strArray2 = str.split("-|\\."); //[how, to, do, in, java] - 5 tokens

1. The String split() Method

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 the result array can be of any size. The trailing empty strings will be discarded.
    • If the limit is negative then the 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

By using regular expressions and character classes in the regular expression, we can split a string based on multiple delimiters.

The following Java program splits a string with multiple delimiters, a hyphen and a dot. We are using regex OR operator '|' symbol between multiple delimiters.

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 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 !!


Notify of
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.