Skip to content

How to Split a String in Java

Sometimes we need to split a string in programming. We suggest String.split(), StringTokenizer, and Pattern.compile() methods.

Splitting a String in Java

How to Split a String in Java by using string split function

java
public class StringSplitTest {
    public static void main(String[] arg) {
        String str = "Welcome:dear guest";
        String[] arrOfStr = str.split(":");
        for (String a : arrOfStr)
            System.out.println(a);
    }
}

The output for this will be like this:

Result

text
Welcome
dear guest

Note Change the regex to get a different result. For example, using the regex "e" will produce the following output:

text
W
lcom
:d
ar gu
st

Also, note that special regex characters (like ., *, +, ?, |, (, ), [, ], {, }, \) should be escaped with a backslash (\) or wrapped in Pattern.quote() to avoid unexpected runtime errors.

1. Using String.split ()

The string split() method breaks a given string around matches of the given regular expression. There are two variants of split() method in Java:

  • public String split(String regex)

This method takes a regular expression as a parameter and breaks the given string around matches of this regular expression. By default limit is 0.

Parameter for this is: regex (a delimiting regular expression).

It returns an array of strings calculated by splitting the given string.

  • public String split(String regex, int limit)

Parameters for this are: regex (the delimiting regular expression) and limit (controls the number of times the pattern is applied and therefore affects the length of the resulting array).

This returns the array of strings counted by splitting this string around matches of the given regular expression.

Example

How to Split a String in Java by using string split function with regular expression and limit

java
public class StringSplitTest {
    public static void main(String[] arg) {
        String str = "Hello:world:hello";
        String[] split = str.split("e", 5);
        for (String s : split)
            System.out.println(s);
    }
}

The output for the given example will be like this:

Result

text
H
llo:world:h
llo

Note: Change regex and limit to have different outputs: e.g. (":", 2)-output will be {Hello, world:hello}; (":", -2)-{Hello, world, hello}, etc.

Let’s see another example:

Example

How to Split a String in Java

java
public class StringSplitTest {
    public static void main(String[] arg) {
        String str = "What are you doing today?";
        String[] split = str.split(" ", 0);
        for (String s : split)
            System.out.println(s);
    }
}

The output will be the following:

Result

How to Split a String in Java

text
What
are
you
doing
Today?

Note: Change regex and limit to have another output: e.g. (" ", 2) - the result will be like this:

Result

How to Split a String in Java

text
What
are you doing today?

Note Setting the limit to 0 discards trailing empty strings from the result.

You can also use String Split with multiple characters using regex.

How to Split a String in Java

java
public class StringSplitTest {
    public static void main(String[] arg) {
        String s = " ;String; String; String; String, String; String;;String;String; String; String; ;String;String;String;String"; //String[] strs = s.split("[,\\s\\;]"); 
        String[] strs = s.split("[,\\;]");
        System.out.println("Substrings length:" + strs.length);
        for (int i = 0; i < strs.length; i++) {
            System.out.println("Str[" + i + "]:" + strs[i]);
        }
    }
}

Output for this will be like this:

Result

How to Split a String in Java

text
Substrings length:17
Str[0]: 
Str[1]:String
Str[2]: String
Str[3]: String
Str[4]: String
Str[5]: String
Str[6]: String
Str[7]:
Str[8]:String
Str[9]:String
Str[10]: String
Str[11]: String
Str[12]: 
Str[13]:String
Str[14]:String
Str[15]:String
Str[16]:String

2. Using StringTokenizer

In Java, the string tokenizer allows breaking a string into tokens. You can also get the number of tokens inside string object.

WarningStringTokenizer is a legacy class. For modern code, prefer String.split() or Pattern.split().

Example

How to Split a String in Java by using StringTokenizer

java
import java.util.StringTokenizer;

public class StringTokenizerExample {
    public static void main(String[] args) {
        StringTokenizer st =
            new StringTokenizer("A StringTokenizer sample");

        // get how many tokens are inside st object
        System.out.println("Tokens count: " + st.countTokens());

        // iterate st object to get more tokens from it
        while (st.hasMoreElements()) {
            String token = st.nextElement().toString();
            System.out.println("Token = " + token);
        }
    }
}

Result

text
Tokens count: 3
Token = A
Token = StringTokenizer
Token = sample

Note: You can specify the delimiter that is used to split the string. In the above example we have set the delimiter as space ().

It is also possible to use StringTokenizer with multiple delimiters.

Example

How to Split a String in Java by using StringTokenizer with multiple delimiters

java
import java.util.StringTokenizer;

public class StringTokenizerExample {
    public static void main(String[] args) {
        String url = "http://www.w3docs.com/learn-javascript.html";
        StringTokenizer multiTokenizer = new StringTokenizer(url, "://.-");
        while (multiTokenizer.hasMoreTokens()) {
            System.out.println(multiTokenizer.nextToken());
        }
    }
}

Result

text
http
www
w3docs
com
learn
javascript
html

Note: In the above-mentioned example :, //, ., - delimiters are used.

3. Using Pattern.compile ()

The split() method of a compiled Pattern splits the given input sequence around matches of the pattern. Parameter for this is: input (the character sequence to be split).

It returns the array of strings computed by splitting the input around matches of the pattern.

Example

How to Split a String in Java by Using Pattern.compile ()

java
import java.util.regex.Pattern;

public class PatternDemo {
    private static String REGEX = ":";
    private static String INPUT = "hello:dear:guest";

    public static void main(String[] args) {
        Pattern pattern = Pattern.compile(REGEX);

        String[] result = pattern.split(INPUT);
        for (String data : result) {
            System.out.println(data);
        }
    }
}

This will produce the following result:

Result

text
hello 
dear 
guest

Dual-run preview — compare with live Symfony routes.