Java Reference
In-Depth Information
Matches ^The \w+ cat.*.
Matches .*
.*.
Matches .*
.*.
Use the replaceFirst() method to create a new String instance in which
the first occurrence of the regular expression in the target text is replaced with the re-
placement text. The code demonstrates how to use this method:
String replaced = jaText.replaceFirst("
",
"mojibake");
System.out.printf("Replaced: %s\n", replaced);
The replacement text is shown in the output:
Replaced: Fight mojibake!
The replaceAll() method replaces all occurrences of the expression with the
replacement text.
Finally, the split() method creates a String[] that contains text that is separ-
ated by the matched expression. In other words, it returns text that is delimited by the
expression. Optionally, you can provide a limit argument that constrains the number
of times the delimiter will be applied in the source text. The following code demon-
strates the split() method splitting on space characters:
String[] matches = enText.split("\\s", 3);
for(String match: matches) {
System.out.printf("Split: %s\n",match);
}
The code's output is as follows:
Split: The
Split: fat
Split: cat sat on the mat with a brown rat.
Solution 2
Search WWH ::




Custom Search