How to Split String in Java

In this post, we will see How to split a string in Java. We are going to cover the below points

  • How to Split a String using a delimiter.
  • How to split String that contains $, *, +, etc.
  • How to Split String using multiple delimiters or Splitting a string in java on more than one symbol.
  • How to Split String array in Java.

How to Split a String using a delimiter.

In this example, we will split a string on basis of the ‘-‘ delimiter.

public class HowToSplitStringInJava {
    public static void main(String[] args) {
        String string = "my-name-is-Jon";
        String[] stringArray = string.split("-");
        for (String str : stringArray) {
            System.out.println(str);
        }
    }
}

Output is –

my
name
is
Jon

How to split String that contains $, *, +, etc.

If we have a String that contains $, *, +, \, ^, ., |, ?, (, ), [, } characters then we need to escape these characters to split the string.

Split a String on basis of $

public class HowToSplitStringInJava {
    public static void main(String[] args) {
        String string = "my$name$is$Jon";
        String[] stringArray = <strong>string.split("\\$");</strong>
        for (String str : stringArray) {
            System.out.println(str);
        }
    }
}

Output is

my
name
is
Jon

We can also use Pattern.quote(“$”) method, in this way no need to escape the characters. We can use it as a String.

import java.util.regex.Pattern;

public class HowToSplitStringInJava {
    public static void main(String[] args) {
        String string = "my$name$is$Jon";
        String[] stringArray = <strong>string.split(Pattern.quote("$"));</strong>
        for (String str : stringArray) {
            System.out.println(str);
        }
    }
}

Output is

my
name
is
Jon

Another way to split String that contains $, * characters.

public class HowToSplitStringInJava {
    public static void main(String[] args) {
        String string = "my$name$is$Jon";
        String[] stringArray = <strong>string.split("[$]");</strong>
        for (String str : stringArray) {
            System.out.println(str);
        }
    }
}

Output is

my
name
is
Jon

How to Split String using multiple delimiters

Consider we have a String ‘my$name*is$Jon$and-i*play=footbal”.

public class HowToSplitStringInJava {
    public static void main(String[] args) {
        String string = "my$name*is$Jon$and-i*play=footbal";
        String[] stringArray = string.split("[-$+*=]");
        for (String str : stringArray) {
            System.out.println(str);
        }
    }
}

Output is

my
name
is
Jon
and
i
play
football

How to Split String array in Java.

We can split an array using the copyOfRange() method.

import java.util.Arrays;
import java.util.regex.Pattern;

public class HowToSplitStringInJava {
    public static void main(String[] args) {
        String stringArray []= {"Jon", "Rombo", "Cersie", "Sansa", "Peter"};//new String [5];

        String[] arrayOne = Arrays.copyOfRange(stringArray, 0, 3);
        System.out.println(arrayOne.length);
        for (String array: arrayOne) {
            System.out.println(array);
        }
    }
}

Output is –

3
Jon
Rombo
Cersie

That’s all about How to Split String in Java.

you may like

Check String split() method docs.