public StringreplaceAll(String regex, String repStr)
Returns a String with all substrings that match the regular expression
regex replaced by repStr. Invoked on str, this is equivalent to
Pattern.compile(regex).matcher(str).replaceAll(repStr).
public String[]split(String regex)
Equivalent to split(regex,0) (see below).
public String[]split(String regex, int limit)
Returns an array of strings resulting from splitting up this string according to
the regular expression. Each match of the regular expression will cause a split
in the string, with the matched part of the string removed. The limit affects
the number of times the regular expression will be applied to the string to
create the array. Any positive number n limits the number of applications to
n1, with the remainder of the string returned as the last element of the array
(so the array will be no larger than n). Any negative limit means that there is
no limit to the number of applications and the array can have any length. A
limit of zero behaves like a negative limit, but trailing empty strings will be
discarded. Invoked on str, this is equivalent to
Pattern.compile(regex).split(str, limit).
This is easier to understand with an example. The following table shows the
array elements returned from split("--",n) invoked on the string
"w--x--y--" for n equal to 1, 0, 1, 2, 3, and 4:
Limit: -1 0 1 2 3 4
Results
[0]: w w w--x--y-- w w w
[1]: x x x--y-- x x
[2]: y y y-- y
[3]: "" ""
With a negative or zero limit we remove all occurrences of "--", with the
difference between the two being the trailing empty string in the negative
case. With a limit of one we don't actually apply the pattern and so the whole
string is returned as the zeroth element. A limit of two applies the pattern
once, breaking the string into two substrings. A limit of three gives us three
substrings. A limit of four gives us four substrings, with the fourth being the
empty string due to the original string ending with the pattern we were
splitting on. Any limit greater than four will return the same results as a limit
of four.
In all the above, if the regular expression syntax is incorrect a PatternSyntaxException is thrown.
These are all convenience methods that avoid the need to work with Pattern and Matcher objects
directly, but they require that the regular expression be compiled each time. If you just want to know if a
given string matches a given regular expression, the matches method returns a boolean to tell you.
Case issues are locale sensitivethat is, they vary from place to place and from culture to culture. The platform
allows users to specify a locale, which includes language and character case issues. Locales are represented by
Locale objects, which you'll learn about in more detail in Chapter 24. The methods toLowerCase and