Java String split()

The split()method in java.lang.String class returns a string array after it splits the given string around matches of a given the regular expression.

In this tutorial, learn how to split a string into an array in Java with the delimiter.

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

System.out.println(Arrays.toString(strArray)); 	// [A, B, C, D]

1. String split() Method

Use split() to split a string into a string array by tokenizing with delimiter or regular expression.

1.1. Method Syntax

The split() method is overloaded:

/**
* @param regex - the delimiting regular expression
* @param limit - the resulting threshold
* 
* @return - the array of strings
*/
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 given example, "[" is invalid regular expression.

public class StringExample 
{
    public static void main(String[] args) 
    {       
        String[] strArray = "hello world".split("[");
    }
}

Program output.

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0
[
^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.clazz(Pattern.java:2548)
	at java.util.regex.Pattern.sequence(Pattern.java:2063)
	at java.util.regex.Pattern.expr(Pattern.java:1996)
	at java.util.regex.Pattern.compile(Pattern.java:1696)
	at java.util.regex.Pattern.<init>(Pattern.java:1351)
	at java.util.regex.Pattern.compile(Pattern.java:1028)
	at java.lang.String.split(String.java:2367)
	at java.lang.String.split(String.java:2409)
	at com.StringExample.main(StringExample.java:9)

1.3. ‘null’ is not valid method argument

Method does not accept 'null' argument. It will throw NullPointerException in case method argument is null.

Exception in thread "main" java.lang.NullPointerException
	at java.lang.String.split(String.java:2324)
	at com.StringExample.main(StringExample.java:11)

2. Java String split() Example

Example 1: Split a string into an array with the given delimiter

Java program to split a string based on a given token. In given example, I am splitting string for delimiter hyphen "-".

public class StringExample 
{
    public static void main(String[] args) 
    {
        String str = "how to do-in-java-provides-java-tutorials";
        
        String[] strArray = str.split("-");
        
        System.out.println(Arrays.toString(strArray));
    }
}

Program output.

[how to do, in, java, provides, java, tutorials]

Example 2: Java split string by whitespace

Java program to split a string by space. In given example, I am splitting string for delimiter hyphen "\\s".

public class StringExample 
{
    public static void main(String[] args) 
    {
        String str = "how to do in java provides java tutorials";
        
        String[] strArray = str.split("\\s");
        
        System.out.println(Arrays.toString(strArray));
    }
}

Program output.

[how, to, do, in, java, provides, java, tutorials]

Example 3: Split a string into an array by comma

Java program to split a string by comma.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String str = "A,B,C,D";
        
        String[] strArray = str.split(",");
        
        System.out.println(Arrays.toString(strArray));
    }
}

Program output.

[A,B,C,D]

Example 4: Java split string 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 hyphen and dot.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String str = "how-to-do-in-java. provides-java-tutorials.";
        
        String[] strArray = str.split("-|\\.");
        
        System.out.println(Arrays.toString(strArray));
    }
}

Program output.

[how, to, do, in, java, provides, java, tutorials]

3. String split(String regex, int limit) Example

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 given the number of tokens, the rest of the 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 as the way the maximum number of tokens can not exceed 5.

public class StringExample 
{
    public static void main(String[] args) 
    {
        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));
    }
}

Program output.

5

[how, to, do, in, java provides java tutorials]

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.