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.

Spliting a String in 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

Welcome
dear guest

Note: Change regex to come to a different result:
E.g. for ("e") regex the result will we like this:

There are many ways to split a string in Java. The most common way is using the split() method which is used to split a string into an array of sub-strings and returns the new array.

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 regex. 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.

Example

W
lcom
:d
ar gu
st
  • 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

public class StringSplitTest {
    public static void 





ain(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

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

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

What
are
you
doing
Today?

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

Result

What
are you doing today?

Set the limit zero to return all the strings matching the regex.

You can also use String Split with multiple characters using regex.
public class StringSplitTest {
    public static void main(String args[]) {
        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

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.

Example

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

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

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

http
www
w3docs
com
learn
javascript
html

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

3. Using Pattern.compile ()

This method 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

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

hello 
dear 
guest

Do you find this helpful?

Related articles