Determines a true or false value for any Java char
value, just as Predicate
does
for any Object
. Also offers basic text processing methods based on this function.
Implementations are strongly encouraged to be side-effect-free and immutable.
Throughout the documentation of this class, the phrase "matching character" is used to mean
"any char
value c
for which this.matches(c)
returns true
".
Warning: This class deals only with char
values; it does not understand
supplementary Unicode code points in the range 0x10000
to 0x10FFFF
. Such logical
characters are encoded into a String
using surrogate pairs, and a CharMatcher
treats these just as two separate characters.
See the Guava User Guide article on CharMatcher
.
- Author:
- Kevin Bourrillion
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionand
(CharMatcher other) Returns a matcher that matches any character matched by both this matcher andother
.static CharMatcher
any()
Matches any character.static CharMatcher
anyOf
(CharSequence sequence) Returns achar
matcher that matches any character present in the given character sequence.static CharMatcher
ascii()
Determines whether a character is ASCII, meaning that its code point is less than 128.int
countIn
(CharSequence sequence) Returns the number of matching characters found in a character sequence.int
indexIn
(CharSequence sequence) Returns the index of the first matching character in a character sequence, or-1
if no matching character is present.int
indexIn
(CharSequence sequence, int start) Returns the index of the first matching character in a character sequence, starting from a given position, or-1
if no character matches after that position.static CharMatcher
is
(char match) Returns achar
matcher that matches only one specified character.static CharMatcher
isNot
(char match) Returns achar
matcher that matches any character except the one specified.static CharMatcher
Determines whether a character is an ISO control character as specified byCharacter.isISOControl(char)
.abstract boolean
matches
(char c) Determines a true or false value for the given character.boolean
matchesAllOf
(CharSequence sequence) Returnstrue
if a character sequence contains only matching characters.boolean
matchesAnyOf
(CharSequence sequence) Returnstrue
if a character sequence contains at least one matching character.boolean
matchesNoneOf
(CharSequence sequence) Returnstrue
if a character sequence contains no matching characters.negate()
Returns a matcher that matches any character not matched by this matcher.static CharMatcher
none()
Matches no character.static CharMatcher
noneOf
(CharSequence sequence) Returns achar
matcher that matches any character not present in the given character sequence.or
(CharMatcher other) Returns a matcher that matches any character matched by either this matcher orother
.
-
Constructor Details
-
CharMatcher
protected CharMatcher()Constructor for use by subclasses. When subclassing, you may want to overridetoString()
to provide a useful description.
-
-
Method Details
-
ascii
Determines whether a character is ASCII, meaning that its code point is less than 128.- Returns:
- a CharMatcher instance that matches ASCII characters
-
isNot
Returns achar
matcher that matches any character except the one specified.To negate another
CharMatcher
, usenegate()
.- Parameters:
match
- the character that should not match- Returns:
- a CharMatcher instance that matches any character except the one specified
-
any
Matches any character.- Returns:
- a CharMatcher that matches any character
-
none
Matches no character.- Returns:
- a CharMatcher that matches no character
-
javaIsoControl
Determines whether a character is an ISO control character as specified byCharacter.isISOControl(char)
.- Returns:
- a CharMatcher that matches ISO control character
-
is
Returns achar
matcher that matches only one specified character.- Parameters:
match
- the character that should match- Returns:
- a CharMatcher that matches the one specified character
-
noneOf
Returns achar
matcher that matches any character not present in the given character sequence.- Parameters:
sequence
- all the characters that should not be matched- Returns:
- a CharMatcher that matches any character not present in the given sequence
-
anyOf
Returns achar
matcher that matches any character present in the given character sequence.- Parameters:
sequence
- all the characters that should be matched- Returns:
- a CharMatcher that matches any character present in the given sequence
-
matches
public abstract boolean matches(char c) Determines a true or false value for the given character.- Parameters:
c
- the character to match- Returns:
true
if thisCharMatcher
instance matches the given character,false
otherwise
-
negate
Returns a matcher that matches any character not matched by this matcher.- Returns:
- new
CharMatcher
instance representing the logical negation of this instance
-
and
Returns a matcher that matches any character matched by both this matcher andother
.- Parameters:
other
- the other instance- Returns:
- new
CharMatcher
instance representing the logical and of this instance and theother
instance
-
or
Returns a matcher that matches any character matched by either this matcher orother
.- Parameters:
other
- the other instance- Returns:
- new
CharMatcher
instance representing the logical and of this instance and theother
instance
-
matchesAnyOf
Returnstrue
if a character sequence contains at least one matching character. Equivalent to!matchesNoneOf(sequence)
.The default implementation iterates over the sequence, invoking
matches(char)
for each character, until this returnstrue
or the end is reached.- Parameters:
sequence
- the character sequence to examine, possibly empty- Returns:
true
if this matcher matches at least one character in the sequence- Since:
- 8.0
-
matchesAllOf
Returnstrue
if a character sequence contains only matching characters.The default implementation iterates over the sequence, invoking
matches(char)
for each character, until this returnsfalse
or the end is reached.- Parameters:
sequence
- the character sequence to examine, possibly empty- Returns:
true
if this matcher matches every character in the sequence, including when the sequence is empty
-
matchesNoneOf
Returnstrue
if a character sequence contains no matching characters. Equivalent to!matchesAnyOf(sequence)
.The default implementation iterates over the sequence, invoking
matches(char)
for each character, until this returnstrue
or the end is reached.- Parameters:
sequence
- the character sequence to examine, possibly empty- Returns:
true
if this matcher matches no characters in the sequence, including when the sequence is empty
-
indexIn
Returns the index of the first matching character in a character sequence, or-1
if no matching character is present.The default implementation iterates over the sequence in forward order calling
matches(char)
for each character.- Parameters:
sequence
- the character sequence to examine from the beginning- Returns:
- an index, or
-1
if no character matches
-
indexIn
Returns the index of the first matching character in a character sequence, starting from a given position, or-1
if no character matches after that position.The default implementation iterates over the sequence in forward order, beginning at
start
, callingmatches(char)
for each character.- Parameters:
sequence
- the character sequence to examinestart
- the first index to examine; must be nonnegative and no greater thansequence.length()
- Returns:
- the index of the first matching character, guaranteed to be no less than
start
, or-1
if no character matches - Throws:
IndexOutOfBoundsException
- if start is negative or greater thansequence.length()
-
countIn
Returns the number of matching characters found in a character sequence.- Parameters:
sequence
- sequence to count the number of matching characters- Returns:
- count of matching characters
-