What class is the split () method a member of?

The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array.

Input String: 016-78967
Regular Expression: - 
Output : {"016", "78967"}

Following are the two variants of the split() method in Java: 

1. Public String [] split ( String regex, int limit)

Parameters:

  • regex – a delimiting regular expression
  • Limit – the resulting threshold

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

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

The limit parameter can have 3 values: 

  • limit > 0 – If this is the case, then the pattern will be applied at most limit-1 times, the resulting array’s length will not be more than n, and the resulting array’s last entry will contain all input beyond the last matched pattern.
  • limit < 0 – In this case, the pattern will be applied as many times as possible, and the resulting array can be of any size.
  • limit = 0 – In this case, the pattern will be applied as many times as possible, the resulting array can be of any size, and trailing empty strings will be discarded.

Here’s how it works:
 
Let the string that is to be split is – geekss@for@geekss

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

Following are the Java example codes to demonstrate the working of split()
 
Example 1:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("@", 2);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 2:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("@", 5);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 3:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("@", -2);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 4:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("s", 5);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 5:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("s", -2);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 6:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "geekss@for@geekss";

        String[] arrOfStr = str.split("s", 0);

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

2. public String[] 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.
 

Parameters: regex – a delimiting regular expression

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

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

Here are some working example codes:
 
Example 1:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "GeeksforGeeks:A Computer Science Portal";

        String[] arrOfStr = str.split(":");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Output

GeeksforGeeks
A Computer Science Portal

Example 2:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "GeeksforGeeksforStudents";

        String[] arrOfStr = str.split("for");

        for (String a : arrOfStr)

            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)

What class is the split () method a member of?

Example 3:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "Geeks for Geeks";

        String[] arrOfStr = str.split(" ");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Example 4:

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "Geeks.for.Geeks";

        String[] arrOfStr = str.split("[.]");  

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

 Example 5: 

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "Geekssss";

        String[] arrOfStr = str.split("s");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

In the above example that trailing empty strings are not included in the resulting array arrOfStr.
  
Example 6:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "GeeksforforGeeksfor   ";

        String[] arrOfStr = str.split("for");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

In the above example, the trailing spaces (hence not empty string) become a string in the resulting array arrOfStr.
 
Example 7:  

Java

public class GFG {

    public static void main(String args[])

    {

        String str = "word1, word2 word3@word4?word5.word6";

        String[] arrOfStr = str.split("[, ?.@]+");

        for (String a : arrOfStr)

            System.out.println(a);

    }

}

Output

word1
word2
word3
word4
word5
word6

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

This article is contributed by Vaibhav Bajpai. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect or if you want to share more information about the topic discussed above.


What is the function of the asterisk (*) in regular expressions?

In regular expressions, asterisk (*) means “match zero or more of the preceding character.” To make a “wildcard” (that is, an expression that matches anything) with regular expressions, you must use '. *' (dot asterisk). This expression means, “match zero or more of any character.”

What symbol S is used to separate multiple exceptions in one catch statement?

Starting from Java 7.0, it is possible for a single catch block to catch multiple exceptions by separating each with | (pipe symbol) in the catch block. Catching multiple exceptions in a single catch block reduces code duplication and increases efficiency.

Which statement determine that Java is a directory?

isDirectory() method would return true only if the file exists and it is an directory. If the file given in the path does not exists then also it return false. So it isDirectory() would return false if the path given does not exists or it exists but it is not a directory...

What are basic regular expression patterns give brief answer for each with example?

Regular expression is not a library nor is it a programming language. Instead, regular expression is a sequence of characters that specifies a search pattern in any given text (string). A text can consist of pretty much anything from letters to numbers, space characters to special characters.