Open In App

Java String split() Method

Last Updated : 24 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

String split() method in Java is used to split a string into an array of substrings based on matches of the given regular expression or specified delimiter. This method returns a string array containing the required substring.

Example: Here, we will Split a String having multiple delimiters or regex into an array of Strings.

// Java program to show the use of split() method
public class Geeks {

    public static void main(String[] args)
    {

        // input string having spaces, comma etc.
        String s = "This is,comma.fullstop  whitespace";

        // The regex [,\\.\\s] splits the string by
        // commas (,), spaces (\\s), and periods (\\.)
        String regex = "[,\\s\\.]";

        // using split() method
        String[] arr = s.split(regex);

        // Print each element of the resulting array
        for (String s1 : arr) {
            System.out.println(s1);
        }
    }
}

Output
This
is
comma
fullstop

whitespace

To know more about regex, please refer to the article – Java Regular Expressions

Now there are two versions of String split() method i.e one with the specified limit and one without any limit.

1. split( String regex, int limit)

This variant of the split() method takes 2 parameter a regular expression as a parameter and a limit that specifies the number of resulting substrings.

public String[] split(String regex, int limit)

Parameters:

  • regex: The regular expression to use as a delimiter.
  • limit (Optional): The number of times the pattern is applied and the array size constraint.

Return Type: An array of strings is computed by splitting the given string.

Exception Thrown: PatternSyntaxException, if the provided regular expression’s syntax is invalid.  

Limit Values:

  • limit > 0: Splits up to limit-1 times, with the rest in the last element.
  • limit < 0: Splits as many times as possible.
  • limit = 0: Splits as many times as possible but discards trailing empty strings.

This method is essential for effective string manipulation, offering control over data processing in Java.

Let the string that is to be split be: geekss@for@geekss

Regex  Limit  Result
@2{“geeks”, ”for@geeks”}
@5{“geeks”, ”for”, ”geeks”} 
@-2{“geeks”, ”for”, ”geeks”}
s    5{“geek”, ”“, “@for@geek”, “”, “”}
s    -2{“geek”, ” “, ” “, “@for@geek”, “”, “”}
s    0{“geek”, ””, ”@for@geek”}

Example 1: Here, we are taking small limit.

// Java program to demonstrate working of
// split(regex, limit) with small limit
public class GFG {

    public static void main(String args[])
    {

        // Custom input string
        String s = "geeks@for@geeks";

        // taking small limit
        String[] arr = s.split("@", 2);

        for (String a : arr)
            System.out.println(a);
    }
}

Output
geeks
for@geeks

Example 2: Here, we are taking high limit.

// Java program to demonstrate working of
// split(regex, limit) with high limit.

public class GFG {

    public static void main(String args[])
    {

        // input string
        String s = "geeks@for@geeks";

        // taking higher limit
        String[] arr = s.split("@", 5);

        for (String a : arr)
            System.out.println(a);
    }
}

Output
geeks
for
geeks

Example 3: Here, we are taking the negative limit.

// Java program to demonstrate working of 
// split(regex, limit) with negative limit.
public class GFG {
  
    public static void main(String args[]){
      
        // taking input string
        String s = "geeks@for@geeks";
      
        // taking negative limit
        String[] arr = s.split("@", -2);

        for (String a : arr)
            System.out.println(a);
    }
}

Output
geeks
for
geeks

Example 4: Here, we are taking limit as zero.

// Java program to demonstrate working of 
// split(regex, limit) with 0 limit.

public class GFG {
  
    public static void main(String args[]) {
      
         // input string
        String s = "geeks@for@geeks";
      
        // taking limit as 0
        String[] arr = s.split("s", 0);

        for (String a : arr)
            System.out.println(a);
    }
}

Output
geek
@for@geek

2. split(String regex)

This variant of the split() method takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. Here, by default limit is 0.

public String[] split(String regex)

Parameter: regex – The regular expression to be used as the delimiter for splitting the string.

Return Type: An array of strings is computed by splitting the given string.

Exception: PatternSyntaxException – If the provided regular expression’s syntax is invalid.  

Example 1: Split String Using Colon

// Java program to demonstrate working 
// of split() method using Colon

public class GFG {
  
    public static void main(String args[]) {
      
        String s = "GeeksforGeeks:A Computer Science Portal";
      
        // split string using colon
        String[] arr = s.split(":");

        for (String a : arr)
            System.out.println(a);
    }
}

Output
GeeksforGeeks
A Computer Science Portal

Example 2: Split String by “for”

// Java program to demonstrate working
// of split() method by "for"
public class GFG {
  
    public static void main(String args[]) {
      
        String s = "GeeksforGeeksforStudents";
      
        // string split
        String[] arr = s.split("for");

        for (String a : arr)
            System.out.println(a);
    }
}

Output
Geeks
Geeks
Students

It can be seen in the above example that the pattern/regular expression “for” is applied twice (because “for” is present two times in the string to be split).

Example 3: Split String by Space

// Java program to demonstrate working
// of split() method by Space
public class GFG {
  
    public static void main(String args[]) {
      
        String s = "Geeks for Geeks";
      
        // split string
        String[] arr = s.split(" ");

        for (String a : arr)
            System.out.println(a);
    }
}

Output
Geeks
for
Geeks

Example 4: Split String by Dot

// Java program to demonstrate working 
// of split() method by dot
public class GFG {
    
  public static void main(String args[]) {
    
        String s = "Geeks.for.Geeks";
    
        // s.split("."); will give
        // no output...
        String[] arr = s.split("[.]"); 

        for (String a : arr)
            System.out.println(a);
    }
}

Output
Geeks
for
Geeks

Example 5: Split String with Trailing Spaces

// Java program to demonstrate working of 
// split() method with trailing spaces
public class GFG {

    public static void main(String args[]) {
      
        String s = "GeeksforforGeeksfor   ";
      
        // split string
        String[] arr = s.split("for");

        for (String a : arr)
            System.out.println(a);
    }
}

Output
Geeks

Geeks
   

In the above example, the trailing spaces (hence not empty string) become a string in the resulting array arr.

Example 6: Split String Using Regular Expression

// Java program to demonstrate working of 
// split() method using regular expressions
public class GFG {

    public static void main(String args[]) {
      
        String s = "w1, w2@w3?w4.w5";
      
        // split string using regex
        String[] arr = s.split("[, ?.@]+");

        for (String a : arr)
            System.out.println(a);
    }
}

Output
w1
w2
w3
w4
w5

In the above example, words are separated whenever either of the characters specified in the set is encountered.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg