Light trails on highway at night

How to split a string in Java

Splitting a string is a very simple task in Java language. There is already built-in functionality that allows you to quickly get the desired results. The String.split() method should fulfill most of the needs. This tutorial will show you various options that can be added to the code.

In addition, at the end of the guide, you'll also see a custom code that may be used to split a string. In most situations, it's recommended to use the standard API. However, you may decide to write your own algorithm to separate a string in case there are some non-standard requirements.

Using the String.split() method

Specification of the split() method is provided below. Please review the following definition to see how exactly this method should be used in your code.

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

The first option is to call the split() method with just a single parameter. You have to provide a regular expression that splits the string. The following sections of this guide contain more details on what kind of regular expression may be used in code.

However, you may also decide to call the split() method with the additional limit parameter. It specifies how many times the regular expression pattern is applied to the string. Examples of how this option works are also provided later in this guide.

Now, we can try to make a very simple application that splits the sentence. As a separator we use the space " " character. Please see the code sample below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "My favorite fruit is banana";

        System.out.println(Arrays.toString(content.split(" ")));
    }
}

Now, if we run the given application, the console output should contain a list of words separated by the comma symbol.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[My, favorite, fruit, is, banana]

Using the String.split() method with limit 1

The following sample is different. We use the limit parameter to specify how many times the separator should be applied.

Let's start with the number 1 for the beginning to see how the split() method works in edge cases. Please see the full code below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "My favorite fruit is banana";

        System.out.println(Arrays.toString(content.split(" ", 1)));
    }
}

If you run that code, you may notice that the string was not divided. There is a single sentence. See it in the console output below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[My favorite fruit is banana]

Using the String.split() method with limit 2

Now, we can try to change the limit parameter to number 2. This should separate the string at least once. The final code is provided below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "My favorite fruit is banana";

        System.out.println(Arrays.toString(content.split(" ", 2)));
    }
}

It's time to run the code. The console output should contain two elements. The word "My" and the rest of the sentence.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[My, favorite fruit is banana]

Using the String.split() method with limit 3

Let's keep on learning how the split() method works. Also, we'd like to try to call it with the number 3. The code is shown below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "My favorite fruit is banana";

        System.out.println(Arrays.toString(content.split(" ", 3)));
    }
}

Now, you may run our code sample. The console output should contain the words "My" and "favorite". Also there will be the rest of the sentence.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[My, favorite, fruit is banana]

Splitting string by white space

The good thing about the split() method is that it accepts regular expressions. It allows you to create a compact and functional code. For example, to split a string by a white space symbol, you may use the "\\s" argument. Example is given below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "My favorite fruit is banana";

        System.out.println(Arrays.toString(content.split("\\s")));
    }
}

If you run the given code, you will see that the whole sentence was separated into parts. See the full console output below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[My, favorite, fruit, is, banana]

Splitting string that contains multiple sequential white-spaces

You may also want to split a string that contains multiple repeating white-spaces. To do that, you have to use the "\\s+" regular expression as a delimiter. Below, you can see how it should be used in the code.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "My favorite    fruit is banana";

        System.out.println(Arrays.toString(content.split("\\s+")));
    }
}

If you run our code sample, you should see a list of separated words as well. You can see the console output below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[My, favorite, fruit, is, banana]

Splitting string that contains multiple delimiters

As you know, regular expressions are very flexible. You may even split a string that contains multiple delimiters. In the following sample, we use the "." and ":" symbols as separators.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "sample:file.txt";

        System.out.println(Arrays.toString(content.split("[.:]")));
    }
}

You may try to execute the given code. The terminal should contain three words: "sample", "file" and "txt".

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[sample, file, txt]

Splitting string into characters

If you need to split the whole string into a list of characters, then the split() method may be used as well. But you need to pass an empty string as an argument. In our case, that's the "" value. Please see the code sample below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "My favorite fruit is banana";

        System.out.println(Arrays.toString(content.split("")));
    }
}

If you run the given code, there should be a comma separated list of symbols in the terminal. Below is our example.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[M, y,  , f, a, v, o, r, i, t, e,  , f, r, u, i, t,  , i, s,  , b, a, n, a, n, a]

Splitting string by specific word

You may also split a string by word. There are no additional options required. You just pass the word into the split() method. Then, that word will be considered as a separator. Please see our code sample below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "MyFavoriteFruitIsBanana";

        System.out.println(Arrays.toString(content.split("Fruit")));
    }
}

Now, you may run our code. The console should contain output like it's shown below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[MyFavorite, IsBanana]

Splitting string by letter

The same way as you split by word, you may also split a string by single letter. That's a simple task as well. You just have to pass the desired letter into the split() method. Our code sample is provided below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "MyFavoriteFruitIsBanana";

        System.out.println(Arrays.toString(content.split("i")));
    }
}

Please run our simple application. The console will contain output like it's shown below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[MyFavor, teFru, tIsBanana]

Splitting string by the dot symbol

The dot symbol (".") is a special character. As a result, if you want to split a string by the dot, then you have to escape it. The value that you may want to pass into the split() method is "\\.". In that case, the string will be divided into parts if there is a dot symbol inside. The full code sample is shown below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "example.txt";

        System.out.println(Arrays.toString(content.split("\\.")));
    }
}

If you run our code sample in terminal, then it should print such values: example, txt.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[example, txt]

Second method of splitting string by the dot symbol

If escaping the dot symbol is not a convenient option for you, then you may use another pattern to split a string. In our sample, we use the dot symbol inside of square brackets. The full source code is provided below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "example.txt";

        System.out.println(Arrays.toString(content.split("[.]")));
    }
}

Now, you may try to run our simple application. The output may be like it's shown below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[example, txt]

Splitting string by the pipe symbol

If you want to split a string by the pipe symbol, then it has to be escaped as well. The correct pattern that must be passed into the split() method is: "\\|". See how it should be used in the following code sample.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "red|green|blue|yellow|pink";

        System.out.println(Arrays.toString(content.split("\\|")));
    }
}

If you run the given code, the console output should contain a comma separated list of color names. You can see our example below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[red, green, blue, yellow, pink]

Splitting string by the plus symbol

The plus sign is a special symbol. And if you want to use it in a regular expression, then it has to be escaped. Here is the pattern that you may pass into the split() method: "\\+". Also, following is our example of how to correctly split a string by the plus sign.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "red+green+blue+yellow+pink";

        System.out.println(Arrays.toString(content.split("\\+")));
    }
}

It's time to run our very simple application. If it's executed, the console output should contain a comma separated list of color names.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[red, green, blue, yellow, pink]

Splitting string by the backslash symbol

The same as it's shown in previous sections of this tutorial, the backslash symbol has to be escaped. The regular expression that you may use to split a string should look like this: "\\\\". As usually, the final code is provided below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "C:\\Files\\Documents\\sample.txt";

        System.out.println(Arrays.toString(content.split("\\\\")));
    }
}

Now, let's see how the above code will work. If you run our sample, the console output should be like this:

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[C:, Files, Documents, sample.txt]

Second method of splitting string by the backslash symbol

In addition, you may put the escaped backslash symbol inside of the square brackets. That string also may be used as a delimiter. The full code sample is shown below.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "C:\\Files\\Documents\\sample.txt";

        System.out.println(Arrays.toString(content.split("[\\\\]")));
    }
}

In such a case, the output will be exactly the same as it's shown in the previous section of this guide.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[C:, Files, Documents, sample.txt]

Splitting string by the new line separator

You may also want to split a string by the new line character. In that case, you should pass that symbol into the split() method. However, that symbol may be different on some systems. To solve that issue, you have to use the System.lineSeparator() method in your application. Below, you can see the correct usage of that method.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "My\nfavorite\nfruit\nis\nbanana";

        System.out.println(content);
        System.out.println(Arrays.toString(content.split(System.lineSeparator())));
    }
}

Now, you may try to run our code. You may notice that initially we print the original string, so you can see that the new line symbols are there. After that, the string is divided into parts and printed into the console as an array of values.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
My
favorite
fruit
is
banana
[My, favorite, fruit, is, banana]

Splitting string by the advanced regular expression

If you need to build complex patterns to separate the strings, it's not an issue at all. You can create advanced regular expressions that may contain more complicated rules. For example, in the following demo, we split the string that contains space, comma or semicolon symbols.

import java.util.Arrays;

class sample {
    public static void main(String args[]) {
        String content = "red,green blue;yellow pink";

        System.out.println(Arrays.toString(content.split("[,\\s\\;]")));
    }
}

If you run our code, you should see a comma separated list of color names in the terminal.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[red, green, blue, yellow, pink]

Handling exception that may occur during splitting the string

The split() method may throw the PatternSyntaxException exception. It may occur if the given regular expression is not in correct format. As a result, you may want to add the try/catch block to the code if there is a risk that some incorrect value may be passed into the split() method.

Below, you can see our code that shows how to handle the exception that may occur when splitting the string.

import java.util.Arrays;
import java.util.regex.PatternSyntaxException;

class sample {
    public static void main(String args[]) {
        try {
            String content = "My favorite fruit is banana";

            System.out.println(Arrays.toString(content.split("*")));
        } catch (PatternSyntaxException exception) {
            exception.printStackTrace();
        }
    }
}

Now, you can try to run the code. You should see the error message like it's shown below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0

Using the Pattern class to split the string

There is an alternative method that allows you to split the string into parts. You may also use the Pattern class. It contains the compile() method which accepts the regular expression.

In our example, we use the code that saves the parts as a list. Please see the demo below.

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

class sample {
    public static void main(String args[]) {
        String content = "My favorite fruit is banana";

        List<String> contentItems = Pattern.compile(" ")
        .splitAsStream(content)
        .collect(Collectors.toList());

        System.out.println(contentItems);
    }
}

If you run our sample, there should be a list of words in the console output. See how it may look below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[My, favorite, fruit, is, banana]

Using the StringTokenizer class to split the string

The StringTokenizer class may also be used to split a string into separate values. However, the issue is that this class is old. It's available only because there may be some projects that still use it. In most cases, it's recommended to use the String.split() method.

But, just in case, we provide a sample that shows how the StringTokenizer class can be used.

import java.util.StringTokenizer;

class sample {
    public static void main(String args[]) {
        String content = "My favorite fruit is banana";

        StringTokenizer contentTokens = new StringTokenizer(content, " ");

        while (contentTokens.hasMoreTokens()){
            String singleItem = contentTokens.nextToken();
            System.out.println(singleItem);
        }
    }
}

The given code should print each word on a new line, as it's shown below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
My
favorite
fruit
is
banana

Using custom method to split a string into two parts

If the standard methods of splitting the string are not acceptable due to some reasons, then you always may write a custom code that will do what you need. However, in most cases, that way is not recommended. The standard API is usually well tested and there is proper documentation.

But, just as an example, you can see a custom code below that will separate a string into just two parts.

import java.util.Arrays;

class MyTools {
    public static String[] splitStringOnce(String source, String delimiter) {
        int position = source.indexOf(delimiter);

        if (position >= 0) {
            String firstPart = source.substring(0, position);
            String secondPart = source.substring(position + 1);

            return new String[]{firstPart, secondPart};
        }

        return null;
    }
}

class sample {
    public static void main(String args[]) {
        String content = "green-blue";

        System.out.println(Arrays.toString(MyTools.splitStringOnce(content, "-")));
    }
}

If you run the given code, it should print two words into the console: green and blue.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[green, blue]

Using custom method to split a string into multiple parts

As you have seen, the previous method splits the strings only into two parts. However, you may also write a custom algorithm that splits the strings into multiple parts. That's a bit more complicated, but possible. Just in case, below you can see our example of the code that will divide the string.

But, as we've already noted, it's recommended to use only the standard API. You use the given code only at your own risk.

import java.util.Arrays;

class MyTools {
    public static String[] splitString(String source, String delimiter) {
        java.util.List<String> sourceParts = new java.util.ArrayList<String>();

        int delimiterPosition = source.indexOf(delimiter);
        int extractPosition = 0;

        while (delimiterPosition != -1) {
            String part = source.substring(extractPosition, delimiterPosition);

            if (part.trim().length() != 0) {
                sourceParts.add(part);
            }

            extractPosition = delimiterPosition + delimiter.length();

            delimiterPosition = source.indexOf(delimiter, extractPosition);
        }

        if (extractPosition < source.length()) {
            String part = source.substring(extractPosition);

            if (part.trim().length() != 0) {
                sourceParts.add(part);
            }
        }

        return sourceParts.toArray(String[]::new);
    }
}

class sample {
    public static void main(String args[]) {
        System.out.println(
            Arrays.toString(
                MyTools.splitString("g", "-")
            )
        );

        System.out.println(
            Arrays.toString(
                MyTools.splitString("g-r", "-")
            )
        );

        System.out.println(
            Arrays.toString(
                MyTools.splitString("g-r-b", "-")
            )
        );
    }
}

Now, you may try to run the given code. The console output should be like it's shown below.

developer@developer-pc:~/samples/java$ javac ./sample.java && java sample
[g]
[g, r]
[g, r, b]

Conclusion

Splitting a string into parts is a trivial task in Java language. In most cases, the standard String.split() method should fulfill your needs. It accepts a regular expression, which means that you can build complex patterns that will divide a string into separate values.

You may also write your own methods to process and split the strings, but usually this option is not recommended. That standard API may work better, since it's very well tested and professionally maintained.

Related Articles

Gray and brown mountain

Converting byte array to string and vice versa in Java

Defocused lights

How to sort arrays in Java

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *