In Java, we can use String#split() to split a string.

String phone = "012-3456789";

// String#split(string regex) accepts regex as the argument
String[] output = phone.split("-");

String part1 = output[0]; // 012
String part2 = output[1]; // 3456789

1. Split a string (default)

1.1 The below example tries to split a string “012-3456789” by a dash -, into two strings.

StringSplit.java

package com.favtuts.string.split;

public class StringSplit {
    
    public static void main(String[] args) {
        String phone = "012-3456789";

        String[] output = phone.split("-");

        System.out.println(output.length);  // 2

        String part1 = output[0]; // 012
        String part2 = output[1]; // 3456789

        System.out.println(part1); // 012
        System.out.println(part2); // 3456789
    }
}

1.2 We can use String#contains to test if the string contains certain characters or delimiters before splitting the string.

StringSplitContains.java

package com.favtuts.string.split;

public class StringSplitContains {
    public static void main(String[] args) {

        String phone = "012-3456789";
  
        if (phone.contains("-")) {
  
            String[] output = phone.split("-");
            System.out.println(output[0]);  // 012
            System.out.println(output[1]);  // 3456789
  
        } else {
            throw new IllegalArgumentException("String " + phone + " does not contain -");
        }
  
    }
}

Output

012
3456789

1.3 For String phone = "0123456789", which doesn’t contain -, it throws IllegalArgumentException.

Exception in thread "main" java.lang.IllegalArgumentException: String 0123456789 does not contain -
  at com.mkyong.string.split.StringSplitContains.main(StringSplitContains.java:16)

2. Split a string and retain the delimiter

The String#spring accepts regex as an argument, and we can also use regex lookahead, lookbehind or lookaround to retain the delimiter.

2.1 Below is a positive lookahead example, the delimiter at the right-hand side.

String[] output = phone.split("(?=-)");
System.out.println(output[0]);  // 012
System.out.println(output[1]);  // -3456789

2.2 Below is a positive lookbehind example, the delimiter at the left-hand side.

String[] output2 = phone.split("(?<=-)");
System.out.println(output2[0]);  // 012-
System.out.println(output2[1]);  // 3456789

2.3 A full example.

StringSplitLookAround.java

package com.favtuts.string.split;

public class StringSplitLookAround {
    public static void main(String[] args) {

        String phone = "012-3456789";

        if (phone.contains("-")) {

            // positive lookahead, split char at right-hand side
            String[] output = phone.split("(?=-)");
            System.out.println(output[0]);  // 012
            System.out.println(output[1]);  // -3456789

            // positive lookbehind, split char at left-hand side
            String[] output2 = phone.split("(?<=-)");
            System.out.println(output2[0]);  // 012-
            System.out.println(output2[1]);  // 3456789

        } else {
            throw new IllegalArgumentException("String " + phone + " does not contain -");
        }

    }
}

Output

012
-3456789
012-
3456789

3. Split a string and limit the split output

The String#split also supports a second argument, which indicates limit, controls the length of the arrays (output).

3.1 The below example try to split a string and limit the output into three strings; the last string contains the remaining inputs after the last matched delimiter (split character).

StringSplitLimit.java

package com.favtuts.string.split;

public class StringSplitLimit {
    public static void main(String[] args) {
        String phone = "012-345-678-9";
        String[] output = phone.split("-", 3);

        System.out.println(output.length);  // 3

        String part1 = output[0];   // 012
        String part2 = output[1];   // 345
        String part3 = output[2];   // 678-9

        System.out.println(part1);
        System.out.println(part2);
        System.out.println(part3);
    }
}

Output

3
012
345
678-9

3.2 The below code snippets try to split a string and limit the output into two strings.

String phone = "012-345-678-9";
String[] output = phone.split("-", 2);

System.out.println(output.length);  // 2

String part1 = output[0];   // 012
String part2 = output[1];   // 345-678-9

4. Split a string by special regex characters

This section talks about escaping the special regex characters as a delimiter.

4.1 Special regex characters

The special regex characters have special meaning as follow:

  1. Period or dot .
  2. Plus sign +
  3. Asterisk or star *
  4. Question mark ?
  5. Caret ^
  6. Dollar sign $
  7. Opening and closing parenthesis ( and )
  8. Opening square bracket [
  9. Opening curly brace {
  10. Pipe symbol |
  11. Backslash \

P.S Both the closing square bracket ] and closing curly brace } is an ordinary character, no need to escape.

4.2 Escaping the special regex characters

If we want to split a string using one of the special regex characters as the delimiter, we must escape it.

For example, if we want to split a string by a vertical bar or pipe symbol | (special regex character), which means or in the regex, we must escape the | symbol using one of the following ways:

  str.split("\\|")              // backslash \ to escape regex special character

  str.split("[|]")              // character class [] to escape regex  special character

  str.split(Pattern.quote("|")) // Pattern#quote() to escape regex special character

4.3 Split a string by a pipe symbol “|”

The below example tries to split a string by a unescape pipe symbol |.

StringSplitSpecial.java

package com.favtuts.string.split;

public class StringSplitSpecial {
    public static void main(String[] args) {
        String csv = "a|b|c|d";
        String[] output = csv.split("|");

        System.out.println(output.length);

        for (String s : output) {
            System.out.println(s);
        }
    }
}

Output

7
a
|
b
|
c
|
d

The below example tries to split a string by an escaped pipe symbol |.

StringSplitSpecialPipe.java

package com.favtuts.string.split;

import java.util.regex.Pattern;

public class StringSplitSpecialPipe {
    public static void main(String[] args) {

        String csv = "a|b|c|d";
  
        // Three ways to escape regex special character
        // String[] output = csv.split("\\|");
        // String[] output = csv.split("[|]");
        String[] output = csv.split(Pattern.quote("|"));

        System.out.println(output.length);
  
        for (String s : output) {
            System.out.println(s);
        }
  
    }
}

4.4 Split a string by a backslash “\”

StringSplitSpecialBackslash.java

package com.favtuts.string.split;

import java.util.regex.Pattern;

public class StringSplitSpecialBackslash {
    public static void main(String[] args) {

        String dir = "C:\\Users\\favtuts\\projects\\favtuts-tutorials";

        // three ways to escape regex special character
        //String[] output = dir.split("\\\\");
        //String[] output = dir.split("[\\\\]");
        String[] output = dir.split(Pattern.quote("\\"));

        for (String s : output) {
            System.out.println(s);
        }

    }
}

Output

C:
Users
favtuts
projects
favtuts-tutorials

4.4 Split a string by a period or dot “.”

StringSplitSpecialPeriod.java

package com.favtuts.string.split;

import java.util.regex.Pattern;

public class StringSplitSpecialPeriod {
    public static void main(String[] args) {

        String dir = "a.b.c.d.e";
  
        // Three ways to escape regex special character
        // String[] output = dir.split("\\.");
        // String[] output = dir.split("[.]");
        String[] output = dir.split(Pattern.quote("."));
  
        for (String s : output) {
            System.out.println(s);
        }
  
    }
}

Output

a
b
c
d
e

5. Split a string by multiple delimiters

If we want to split a string by multiple delimiters, just put all the delimiters into brackets [ ].

StringSplitMultiDelimiters.java

package com.favtuts.string.split;

public class StringSplitMultiDelimiters {
    public static void main(String[] args) {

        String dir = "apple|9|1.88;2.78|0#10";
        String[] output = dir.split("[|;#]");
  
        System.out.println(output.length);  // 6
  
        for (String s : output) {
            System.out.println(s);
        }
  
        /*
        System.out.println(output[0]);      // apple
        System.out.println(output[1]);      // 9
        System.out.println(output[2]);      // 1.88
        System.out.println(output[3]);      // 2.78
        System.out.println(output[4]);      // 0
        System.out.println(output[5]);      // 10
        */
  
    }
}

Output

6
apple
9
1.88
2.78
0
10

6. Split a string and Java 8 splitAsStream

6.1 For Java 8 stream, we can use Pattern#splitAsStream to split a string and return a stream.

StringSplitJava8.java

package com.favtuts.string.split;

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

public class StringSplitJava8 {
    public static void main(String[] args) {

        String phone = "012-3456789";

        // Normal String#split
        // String[] output = phone.split("-");

        // Java 8 splitAsStream
        List<String> output = Pattern.compile("-")
                .splitAsStream(phone)
                .collect(Collectors.toList());

        System.out.println(output);
    }
}

Output

[012, 3456789]

6.2 We also can use Arrays.stream to convert a split array into a stream.

SplitString5b.java

package com.favtuts.string.split;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SplitString5b {
    public static void main(String[] args) {

        String phone = "012-3456789";

        List<String> output = Arrays.stream(phone.split("-"))
                .collect(Collectors.toList());

        System.out.println(output);


    }
}

Output

[012, 3456789]

7. Split a string by a space or multiple spaces

We can use \\s+ (which means one or more whitespace characters in regex) to split a string by spaces.

StringSplitSpaces.java

package com.favtuts.string.split;

public class StringSplitSpaces {
    public static void main(String[] args) {

        String str = "1 2   3 4  5";
        String[] output = str.split("\\s+");

        for (String s : output) {
            System.out.println(s);
        }

    }
}

Output

1
2
3
4
5

8. Split a string by new lines

The different operating system has a different new line format:

  • Unix, Linux or Mac \r
  • Windows \r\n

The example uses the regex pattern \\r?\\n to split a string by new lines and return a stream; later, we trim and filter the empty lines.

StringSplitNewLines.java

package com.favtuts.string.split;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StringSplitNewLines {
    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder();
        sb.append("aaa \n");
        sb.append("bbb   \r\n");
        sb.append("ccc\n");
        sb.append("\n");
        sb.append("ddd\r\n");
        sb.append("\r\n");
        sb.append("eee\n");

        String text = sb.toString();
        System.out.println("---Original---");
        System.out.println(text);

        System.out.println("---Split---");

        // split by new line, trim and filter empty line
        List<String> lines = Arrays.stream(text.split("\\r?\\n"))
                .map(x -> x.trim())
                .filter(x -> x.length() > 0)
                .collect(Collectors.toList());

        for (String line : lines) {
            System.out.println(line);
        }

    }

}

Output

---Original---
aaa
bbb   
ccc

ddd

eee

---Split---
aaa
bbb
ccc
ddd
eee

9. Split a string by StringTokenizer (legacy)

Java developers used the StringTokenizer class to split a string.

P.S The String#split() is available since JDK 1.4.

Note

This StringTokenizer is a legacy class, retained for compatibility reasons; the use is discouraged! Please upgrade the code to String#split().

StringSplitStringTokenizer.java

package com.favtuts.string.split;

import java.util.StringTokenizer;

public class StringSplitStringTokenizer {
    public static void main(String[] args) {

        String test = "abc.def.123";
        // the delimiter is a string, not regex, no need to escape the dot
        StringTokenizer token = new StringTokenizer(test, ".");

        while (token.hasMoreTokens()) {
            System.out.println(token.nextToken());
        }

    }
}

Output

abc
def
123

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-string

References

Leave a Reply

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