Java String split()

The method split() is used for splitting a String into its substrings based on the given delimiter/regular expression. This method has two variants:

String[] split(String regex): It returns an array of strings after splitting an input String based on the delimiting regular expression.

String[] split(String regex, int limit): The only difference between above variation and this one is that it limits the number of strings returned after split up. For e.g. split("anydelimiter", 3) would return the array of only 3 strings even through the delimiter is present in the string more than 3 times. If the limit is negative then the returned array would be having as many substrings as possible however when the limit is zero then the returned array would be having all the substrings excluding the trailing empty Strings.

Internal implementation
public String[] split(String regex, int limit) { char ch = 0; if (((regex.value.length == 1 && ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) || (regex.length() == 2 && regex.charAt(0) == '\\' && (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 && ((ch-'a')|('z'-ch)) < 0 && ((ch-'A')|('Z'-ch)) < 0)) && (ch < Character.MIN_HIGH_SURROGATE || ch > Character.MAX_LOW_SURROGATE)) { int off = 0; int next = 0; boolean limited = limit > 0; ArrayList list = new ArrayList<>(); while ((next = indexOf(ch, off)) != -1) { if (!limited || list.size() < limit - 1) { list.add(substring(off, next)); off = next + 1; } else { // last one list.add(substring(off, value.length)); off = value.length; break; } } if (off == 0) return new String[]{this}; if (!limited || list.size() < limit) list.add(substring(off, value.length)); int resultSize = list.size(); if (limit == 0) while (resultSize > 0 && list.get(resultSize - 1).length() == 0) resultSize--; String[] result = new String[resultSize]; return list.subList(0, resultSize).toArray(result); } return Pattern.compile(regex).split(this, limit); }
Parameter

regex : regular expression to be applied on string.

limit : limit for the number of strings in array. If it is zero, it will returns all the strings matching regex.

Returns

array of strings

Throws

PatternSyntaxException if pattern for regular expression is invalid

Public String [ ] split ( String regex, int limit )
Parameters: regex - a delimiting regular expression Limit - the result threshold Returns: An array of strings computed by splitting the given string. Throws: PatternSyntaxException - if the provided regular expression’s syntax is invalid.
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 to be splitted be : 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”}
Example: split() method
public class Test { public static void main(String args[]) { String str = new String("10/09/2018"); System.out.println("split(String regex):"); String array1[]= str.split("/"); for (String temp: array1){ System.out.println(temp); } System.out.println("split(String regex, int limit) with limit=2:"); String array2[]= str.split("/", 2); for (String temp: array2){ System.out.println(temp); } System.out.println("split(String regex, int limit) with limit=0:"); String array3[]= str.split("/", 0); for (String temp: array3){ System.out.println(temp); } System.out.println("split(String regex, int limit) with limit=-5:"); String array4[]= str.split("/", -5); for (String temp: array4){ System.out.println(temp); } } }


Output:
split(String regex): 10 09 2018 split(String regex, int limit) with limit=2: 10 09/2018 split(String regex, int limit) with limit=0: 10 09 2018 split(String regex, int limit) with limit=-5: 10 09 2018
Example
public class Test { public static void main(String args[]) { String str = "cprogramcoding@for@cprogramcoding"; String[] arr= str.split("@", 2); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding for@cprogramcoding
Example
public class Test { public static void main(String args[]) { String str = "cprogramcoding@for@cprogramcoding"; String[] arr = str.split("@", 5); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding for cprogramcoding
Example
public class Test { public static void main(String args[]) { String str = "cprogramcoding@for@cprogramcoding"; String[] arr = str.split("@", -2); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding for cprogramcoding
Example
public class Test { public static void main(String args[]) { String str = "cprogramcoding@for@cprogramcoding"; String[] arr = str.split("s", 5); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding@for@cprogramcoding
Example
// Java program to demonstrate working of split(regex, // limit) with negative limit. public class Test { public static void main(String args[]) { String str = "cprogramcoding@for@cprogramcoding"; String[] arr = str.split("s", -2); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding@for@cprogramcoding
Example
// Java program to demonstrate working of split(regex, // limit) with 0 limit. public class Test { public static void main(String args[]) { String str = "cprogramcoding@for@cprogramcoding"; String[] arr = str.split("s", 0); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding@for@cprogramcoding
Java String split() method example

The given example returns total number of words in a string excluding space only. It also includes special characters.

public class Test { public static void main(String args[]) { String s1="java string split method by cprogramcoding"; String[] words=s1.split("\\s"); for(String w:words){ System.out.println(w); } } }


Output:
java string split method by cprogramcoding
Java String split() method with regex and length example
public class Test { public static void main(String args[]) { String s1="welcome to split world tutorial in cprogramcoding"; System.out.println("returning words:"); for(String w:s1.split("\\s",0)){ System.out.println(w); } System.out.println("returning words:"); for(String w:s1.split("\\s",1)){ System.out.println(w); } System.out.println("returning words:"); for(String w:s1.split("\\s",2)){ System.out.println(w); } } }


Output:
returning words: welcome to split world tutorial in cprogramcoding returning words: welcome to split world tutorial in cprogramcoding returning words: welcome to split world tutorial in cprogramcoding
Java String split() method with regex and length example

Here, we are passing split limit as a second argument to this function. This limits the number of splitted strings.

public class Test { public static void main(String[] args) { String str = "cprogramcoding"; System.out.println("Returning words:"); String[] arry = str.split("t", 0); for (String p : arry) { System.out.println(p); } System.out.println("Split array length: "+arry.length); } }


Output:
Returning words: cprogramcoding Split array length: 1
public String[] split(String regex)

This variant of split method takes a regular expression as 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 computed by splitting the given string. Throws: PatternSyntaxException - if the provided regular expression’s syntax is invalid.
Example
// Java program to demonstrate working of split() public class Test { public static void main(String args[]) { String str = "cprogramcodingforcprogramcoding:A Computer Science Portal"; String[] arr = str.split(":"); for (String p : arr) System.out.println(p); } }


Output:
cprogramcodingforcprogramcoding A Computer Science Portal
Example
// Java program to demonstrate working of split() public class Test { public static void main(String args[]) { String str = "cprogramcodingforStudents"; String[] arr = str.split("for"); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding Students
Example
// Java program to demonstrate working of split() public class Test { public static void main(String args[]) { String str = "cprogramcoding for cprogramcoding"; String[] arr = str.split(" "); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding for cprogramcoding
Example
// Java program to demonstrate working of split() public class Test { public static void main(String args[]) { String str = "cprogramcoding"; String[] arr = str.split("s"); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding

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

Example
public class Test { public static void main(String args[]) { String str = "cprogramcodingforforcprogramcodingfor "; String[] arr = str.split("for"); for (String p : arr) System.out.println(p); } }


Output:
cprogramcoding cprogramcoding

In the above example, the trailing spaces (hence not empty string) in the end becomes a string in the resulting array arrOfStr.

Example
// Java program to demonstrate working of split() // using regular expressions public class Test { public static void main(String args[]) { String str = "program1, program2 program3@program4?program5.program6"; String[] arr = str.split("[, ?.@]+"); for (String p : arr) System.out.println(p); } }


Output:
program1 program2 program3 program4 program5 program6



Instagram