A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/split-string-java-examples/ below:

Java String split() Method - GeeksforGeeks

Java String split() Method

Last Updated : 23 Jul, 2025

The 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
// 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

Syntax of split() Method

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

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

public String[] split(String regex)

Parameters:

Return Type:

Exception:

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.

Limit Values:

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: Java program to split a string using split(String regex, int limit) with the specified low limit value.

Java
// Java program to demonstrate working of
// split(regex, limit) with small limit
public class Geeks
{
    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);
    }
}

Example 2: Java program to split a string using split(String regex, int limit) with the specified high limit value.

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

public class Geeks 
{

    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);
    }
}

Example 3: Java program to split a string using split(String regex, int limit) with the specified negative limit value.

Java
// Java program to demonstrate working of 
// split(regex, limit) with negative limit.
public class Geeks
{
    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);
    }
}

Example 4: Java program to split a string using split(String regex, int limit) with the specified limit value as zero.

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

public class Geeks
{
  
    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);
    }
}
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.

Example 1: Java program to split String by colon using split(String regex) method.

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

public class Geeks
{
  
    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: Java program to split String by a specific string (by) using split(String regex) method.

Java
// Java program to demonstrate working
// of split() method by "for"
public class Geeks
{
    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: Java program to split String by space(" ") using split(String regex) method.

Java
// Java program to demonstrate working
// of split() method by Space
public class Geeks
{
  
    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);
    }
}

Example 4: Split String by Dot

Java
// Java program to demonstrate working 
// of split() method by dot
public class Geeks
{
    
  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);
    }
}

Example 5: Split String with Trailing Spaces

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

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

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

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
// Java program to demonstrate working of 
// split() method using regular expressions
public class Geeks
{

    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);
    }
}

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

Example 7: Using split() method with the case where delimiter/regex is not present in the string.

Java
// Java program to demonstrate the case where delimiter is not found
public class Geeks
{
  
    public static void main(String args[]) {
      
        String s = "GeeksforGeeks";  
      
        // Split string using delimiter "#" which is not in the string
        String[] arr = s.split("#");
        
        // Print each element of the resulting array
        for (String a : arr)
            System.out.println(a);
    }
}

In the above code, if the delimiter is not present in the string, we get the same string output as original with no changes.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4