Java split Method

Educational infographic highlighting essential aspects of Java's split method. Covering delimiter tricks, whitespace handling, and advanced regex patterns.

What is Java split method?

  • The split method breaks the specified string by the given regular expression delimiter and returns an array.
  • The array contains substrings of the specified string after splitting.
  • For example, you may break a string containing commas:
String text = "apple,orange,banana";
String[] fruits = text.split(",");

Syntax of split Java method

Following are the ways of using the split method:

split(String regex)
  • In this way of using the split method, the complete string will be broken.
  • The regex (regular expression) defines the pattern. It can be a character like space, comma, complex expressions with special characters, etc. You may learn more about regex constructs.
  • The split function returns an array of broken strings that you may manipulate just like the normal array in Java.

You may also use this method as follows:

split(String regex, int limit)

That is, limit the number of splits in the given string by using the limit argument. The limit is given as int.

See the section below for examples with code and output.

An example of completely breaking the string by split method

For demonstrating the Java string split method, a string object is created and assigned a value.

In the split method, the delimiter used for breaking the string is a space character. As no limit parameter is provided, so complete string will break:

public class split_demo {

          public static void main(String args[]) {

                

                    String str_split = new String("The String split method");

                    System.out.println("The string after split :\n" );     

                   

                    //Using split method with a space regex

                    for (String str: str_split.split(" ")) {

                       System.out.println(str);

                    }

                 }

}

Output:

The string after split :
The
String
split
method

You see, the string is broken into four pieces. The for loop is used to iterate through the returned array to display its items.

Note: You may also use \\s regex for the space character.

Using the string split with limit parameter example

Suppose, you want to break the phone numbers that are in string format as follows:

456-434-8987

You just need to break this from first dash character while keeping the second intact. This is where limit parameter can play its role. Otherwise, simply using the split method will break the string completely; in that case, in three pieces. See the code below:

public class split_demo {

          public static void main(String args[]) {

                

                    String str_tel = "458-758-8975";

                    System.out.println("The Phone after split with limit :\n" );     

                   

                    //Using split method with a dash regex

                    for (String str: str_tel.split("-",2)) {

                       System.out.println(str);

                    }

                 }

}

Output:

The Phone after split with limit :
458
758-8975

Using dot regex for splitting a string example

Similarly, you may use dot (.) as the character where you want to break the given string. Suppose, you have long paragraphs and the requirement is to break the paragraphs into sentences based on full stops. Here is how you may do this:

public class split_demo {

          public static void main(String args[]) {

                

                    String str_split = new String("Sentence 1 in paragraph. Sentence 2 in paragraph. Sentence 3 in paragraph");

                   

                    //with a dot regex

                    for (String str_dot: str_split.split("\\. ")) {

                       System.out.println(str_dot);

                    }

                 }

}

Output:

Sentence 1 in paragraph
Sentence 2 in paragraph
Sentence 3 in paragraph

string split dot

Note: the simple dot will not work. You have to escape the dot in order to break the string paragraphs based on full stop.

That means:

str_split.split(“\\. “) will work.

str_split.split(“. “) will not work.

A little complex regex for multiple delimiters demo

In some scenarios, you may need to split a string for multiple delimiters.

For example, break the string with space, commas, hyphens, or other characters in one call.

As such, the Java split method performs split based on a given regular expression, so you may create such a regex that may accommodate all.

See this example where I will split the string with space, comma, dot, and dash by using regex:

public class split_demo {

       public static void main (String[] args)

    {

              String str_split="split method.It is cool - (Hello-World)";

        for (String str_complex : str_split.split("\\s|,|\\.|-")) {

            System.out.println(str_complex);

        }

    }

}

 

The output of the above code:

split
method
It
is
cool

(Hello
World)

Note: The “|” represents OR in regex.

Similarly, you may create your own regex as per the requirement of the project.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!