How to Split a String in Java: Different Examples

This tutorial will teach you how to split a string in Java. There is more than one way to split a string, so we will look at several ways.

Let’s start with a very basic one.

Split a String

To split a string in Java, you can use a String class’s split() method. The split() method accepts a regular expression as a method argument and then uses that regular expression to split a String. The return value is an array of Strings.

String[] charactersArray = characters.split(",");

In the code example below, the split() method uses a comma as a delimiter to split the string.

public class App {
    public static void main(String[] args) {
        String characters = "a,b,c,d";

        String[] charactersArray = characters.split(",");

        for (String character : charactersArray) {
            System.out.println(character);
        }
    }
}

The output is:

a
b
c
d

Split a String and Limit the Resulting Array

The split() method can also accept the limit as a second parameter.

String[] charactersArray = characters.split(",", limit);

You can use the limit parameter to limit the number of parts you want the resulting array to contain. For example, in the below code example, we will split the string “a,b,c,d” into an array of 3 items.

public class App {
    public static void main(String[] args) {
        String characters = "a,b,c,d";

        String[] charactersArray = characters.split(",", 3);

        for (String character : charactersArray) {
            System.out.println(character);
        }
    }
}

The output is:

a
b
c,d

Notice that the resulting array contains exactly 3 items:

  1. a,
  2. b,
  3. c,d

Spring String by Space or Dash Character

The delimiter by which you want to split a String can be a regular expression or a simple character like comma(-), dash(-) or a blank space character. In the examples above, we have used a comma as a delimiter to split a String. Let’s assume we have a phone number and need to split it using the dash character. In this case, our code will look the following way.

Split a String by a Dash

String phoneNumber = "613-898-3356";
String[] charactersArray = phoneNumber.split("-");

Spring a String by a Blank Space

If we need to split a String using a blank space character, then our code will look like this.

String phoneNumber = "613 898 3356"; 
String[] charactersArray = phoneNumber.split(" ");

Unfortunately, not any character can be used as is as a delimiter. Some charters are reserved by regex expressions. But if needed, you can still use those special characters, provided that you escape them. Let’s learn how to do it in the following section.

Split a String by Special regex Character

The first parameter the split() method accepts is a regular expression. And in the above code examples, as a delimiter to split a string, we have used a comma(,). Fortunately, comm is not a special character in the regular expression syntax. But if we used a dot(.) or a backslash character(\), we would have to escape it.

Special characters in regular expressions are: ., +, *, ?, ^, $, (, ), [, ], {, }, |, \.

For example, let’s assume that we need to split the following string “a.b.c.d” using dot(.) as a delimiter. Dot is a special character in regular expressions, and this means that we need to escape it to be able to split the string. To escape a special character, you can add two forward slashes(\\) as a prefix.

String[] charactersArray = characters.split("\\.");

Let’s have a look at a complete example.

public class App {
    public static void main(String[] args) {
        String characters = "a.b.c.d";
        String[] charactersArray = characters.split("\\.");
        for (String character : charactersArray) {
            System.out.println(character);
        }
    }
}

The output is:

a
b
c
d

Pattern.quote() as a way to escape regex special characters

There are other ways you can escape a regex special character. For example, instead of using two backslashes, you can use Pattern.quote(delimiter) method. In this case, the delimiter will be treated as an ordinary character.

String[] charactersArray = characters.split(Pattern.quote("."));

Split a String but Keep Delimiter

Regular expressions are a very powerful way to split a string with delimiters. For example, you can write a regular expression that will split the string but will keep a delimiter. The technique is called lookahead and lookbehind assertions.

Keep delimiter on the right side

If you need to split a string but to keep the delimiter on the right side, use the following regular expression.

String characters = "a.b.c";
String[] charactersArray = characters.split("(?=\\.)");

Keep delimiter on the left side

Use the following regular expression to keep the delimiter on the left side.

String characters = "a.b.c";
String[] charactersArray = characters.split("(?<=\\.)");

Let’s have a look at the complete example.

import java.util.regex.Pattern;

public class App {
    public static void main(String[] args) {
        String characters = "a.b.c";
        String[] charactersArray = characters.split("(?<=\\.)");
        for (String character : charactersArray) {
            System.out.println(character);
        }
    }
}

The output is:

a.
b.
c

I hope this very short blog post was helpful to you.

There are many other useful tutorials you can find on this site. To find Java-related tutorials, check out the Java tutorials page.

Happy learning!