bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java String split() Method

Flash cards

Review the key moves

1/4
Core idea

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.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ myStr = "Split a string by spaces, and also punctuation.";
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The split() method splits a string into an array of substrings using a regular expression as the separator.
Definition and Usage
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