Flash cards
Review the key moves
What is the main idea behind Java String split() Method?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
___ myStr = "Split a string by spaces, and also punctuation.";Put the learning moves in the order that makes the concept easiest to apply.
❮ 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
| Parameter | Description |
|---|---|
| regex | Required. A regular expression defining the separators where the string is split. |
| limit | Optional. 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 |