Java Reference
In-Depth Information
Using the split method
We demonstrated the String class' split method in Chapter 1 , Introduction to NLP . It
is duplicated here for convenience:
String text = "Mr. Smith went to 123 Washington avenue.";
String tokens[] = text.split("\\s+");
for (String token : tokens) {
System.out.println(token);
}
The output is as follows:
Mr.
Smith
went
to
123
Washington
avenue.
The split method also uses a regular expression. If we replace the text with the same
string we used in the previous section, "Let's pause, and then reflect.", we will get the same
output.
The split method has an overloaded version that uses an integer to specify how many
times the regular expression pattern is applied to the target text. Using this parameter can
stop the operation after the specified number of matches has been made.
The Pattern class also has a split method. It will split its argument based on the pat-
tern used to create the Pattern object.
Search WWH ::




Custom Search