.split()
The .split()
method takes a string and splits it into an array of substrings based on a pattern delimiter. The pattern provided is a regular expression that will split the original string based on where the pattern is matched in the string.
The method will throw a PatternSyntaxException
for a pattern that is an invalid regular expression.
Syntax
String[] result = string.split(delimiter, limit);
Where string
is the string to be split, and delimiter
is a string containing a regular expression pattern.
The optional limit
parameter is an integer, and has the following behavior:
limit
> 0:.split()
will return at mostlimit
entries, stopping afterlimit
-1 matches.limit
< 0:.split()
will match as many times as possible, resulting in an array of any size.limit
= 0:.split()
is the same aslimit
< 0 except that trailing empty strings will be discarded.
Example 1
The following uses the regular expression “\s+” which matches one or more whitespace characters.
public class Example{public static void main(String args[]){String words = "This is a string of words.";String[] result = words.split("\\s+"); // Splits words on matching sequences of whitespace.for(String w:result){System.out.println(w);}}}
This outputs the following:
Thisisastringofwords.
Example 2
This example uses the pattern “,\s*“ which matches a comma followed by zero or more whitespace characters, it also uses a limit parameter.
public class Example{public static void main(String args[]){String words = "This, is, a, comma,delimited, list, of, words";String[] result = words.split(",\\s*",6); // Splits words on matching commas followed by arbitrary whitespace.for(String w:result){System.out.println(w);}}}
This outputs the following:
Thisisacommadelimitedlist, of, words
Example 3
This example shows the difference between a negative limit and a limit of zero.
public class Example{public static void main(String args[]){String words = "This,string,has,extra,commas,,,,";String[] result1 = words.split(",",-2); // Split on each comma.String[] result2 = words.split(",",0); // Split on each comma, but discard trailing empty strings.System.out.println("Limit of -2 produces an array of length " + result1.length);System.out.println("Limit of 0 produces an array of length " + result2.length);}}
This outputs the following:
Limit of -2 produces an array of length 9Limit of 0 produces an array of length 5
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.