bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java String split() Method

❮ String Methods

Example

String myStr = "Split a string by spaces, and also punctuation.";
String regex = "[,\\.\\s]";
String[] myArray = myStr.split(regex);
for (String s : myArray) {
  System.out.println(s);
}

Definition and Usage

The split() method splits a string into an array of substrings using a regular expression as the separator.

If a limit is specified, the returned array will not be longer than the limit. The last element of the array will contain the remainder of the string, which may still have separators in it if the limit was reached.

Tip

See the Java RegEx tutorial to learn about regular expressions.

One of the following

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

Parameter Values

ParameterDescription
regexRequired. A regular expression defining the separators where the string is split.
limitOptional. The maximum length of the returned array.

Technical Details

Returns:A String array.
Throws:PatternSyntaxException - If the syntax of the regular expression is incorrect.
Java version:1.4

Previous

Java String replaceFirst() Method

Next

Java String startsWith() Method