Skip to content

Java string split Method | Regex With Space, Comma, Dot Examples

  • by

A Java string Split methods have used to get the substring or split or char from String. The split method works on the Regex matched case, if matched then split the string sentences.

You can split a string with many time regular expressions, here are some Splitting Regex can be:

  • Space (Whitespace) – (“\\s”)
  • Comma (“,”)
  • Dot (“\\.”)
Java string split Method Regex With Space, Comma, Dot Examples

Syntax

There is two Syntax of the split() method in a Java string.

public String split(String regex)  
//or  
public String split(String regex, int limit)

Parameter

  • regex: regular expression required to splitting point.
  • limit: Number of strings in Array, if the value is 0 (zero) then it will do splitting on complete strings

Return Value: 

The split () method will return a String Arrays.

Java string split example (space)

In the example First, create and assign the string variable “str1“. Then create a string array and split the str1 with space.

The method returns the Array string and stores the value in strArray. Then do iteration with Java for each loop and print the values. Array indexing starts with zero.

The example and program of Java string split with space.

public class SplitExample {
    public static void main(String args[]) {
        String str1 = "How to split Strings in Java";
        //splits the string where is space
        String[] strArray = str1.split("\\s");
        for (String w : strArray) {
            System.out.println(w);
        }
    }
}

Output: How
to
split
Strings
in
Java

string split with Comma ( , ) in Java

An upper example has split the string with space now see the example of how to do it with Comma. We are using Java for loop in this example. For its very easy regex just pass like that, Regex - (",")

public class SplitExample {

    public static void main(String args[]) {
        String str = "Split,method,with,comma";
        String[] strArray = str.split(",");
        //getting string Array
        for (int i = 0; strArray.length > i; i++) {
            System.out.println(strArray[i]);
        }
    }
}

Output: Split
method
with
comma

string split with a Dot  ( . ) in Java

Let’s Do the same thing with dot example in Java. For that, you have to pass the regex - ("\\.")

String str = "Split.method.with.dot";
        String[] strArray = str.split("\\.");
        //getting string Array
        for (int i = 0; strArray.length > i; i++) {
            System.out.println(strArray[i]);
        }

Output: Split
method
with
dot

How Java String split() method with regex and length example.

Let’s do Use and see how it’s work with when splitting a String in Java.

public class SplitExample {

    public static void main(String args[]) {
        String s1 = "Split method with regex length example";
        System.out.println("--- Zero Length ---");
        for (String strArray : s1.split("\\s", 0)) {
            System.out.println(strArray);
        }
        System.out.println("--- Two Length ---");
        for (String strArray : s1.split("\\s", 2)) {
            System.out.println(strArray);
        }
        System.out.println("--- Four Length ---");
        for (String strArray : s1.split("\\s", 4)) {
            System.out.println(strArray);
        }

    }
}

Output Screenshot:

How Java String split() method with regex and length example output

 

QA: How to get a Single value from strings after splitting?

It’s a very easy just pass the index value in string array after a splitting. Here is the example code of getting the first value form string.

String str = "Split method with regex length example";
        String[] strArray = str.split("\\s");
        //getting first value form string
        System.out.println(strArray[0]);

Output: Split

Sidenotes:  As the Same example of a Split method with regex and length, you can do with number or string to split a strings sentences in java.

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java string split Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading