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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionand(CharMatcher other) Returns a matcher that matches any character matched by both this matcher andother.static CharMatcherany()Matches any character.static CharMatcheranyOf(CharSequence sequence) Returns acharmatcher that matches any character present in the given character sequence.static CharMatcherascii()Determines whether a character is ASCII, meaning that its code point is less than 128.intcountIn(CharSequence sequence) Returns the number of matching characters found in a character sequence.intindexIn(CharSequence sequence) Returns the index of the first matching character in a character sequence, or-1if no matching character is present.intindexIn(CharSequence sequence, int start) Returns the index of the first matching character in a character sequence, starting from a given position, or-1if no character matches after that position.static CharMatcheris(char match) Returns acharmatcher that matches only one specified character.static CharMatcherisNot(char match) Returns acharmatcher that matches any character except the one specified.static CharMatcherDetermines whether a character is an ISO control character as specified byCharacter.isISOControl(char).abstract booleanmatches(char c) Determines a true or false value for the given character.booleanmatchesAllOf(CharSequence sequence) Returnstrueif a character sequence contains only matching characters.booleanmatchesAnyOf(CharSequence sequence) Returnstrueif a character sequence contains at least one matching character.booleanmatchesNoneOf(CharSequence sequence) Returnstrueif a character sequence contains no matching characters.negate()Returns a matcher that matches any character not matched by this matcher.static CharMatchernone()Matches no character.static CharMatchernoneOf(CharSequence sequence) Returns acharmatcher 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 acharmatcher 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 acharmatcher that matches only one specified character.- Parameters:
match- the character that should match- Returns:
- a CharMatcher that matches the one specified character
-
noneOf
Returns acharmatcher 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 acharmatcher 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:
trueif thisCharMatcherinstance matches the given character,falseotherwise
-
negate
Returns a matcher that matches any character not matched by this matcher.- Returns:
- new
CharMatcherinstance 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
CharMatcherinstance representing the logical and of this instance and theotherinstance
-
or
Returns a matcher that matches any character matched by either this matcher orother.- Parameters:
other- the other instance- Returns:
- new
CharMatcherinstance representing the logical and of this instance and theotherinstance
-
matchesAnyOf
Returnstrueif 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 returnstrueor the end is reached.- Parameters:
sequence- the character sequence to examine, possibly empty- Returns:
trueif this matcher matches at least one character in the sequence- Since:
- 8.0
-
matchesAllOf
Returnstrueif a character sequence contains only matching characters.The default implementation iterates over the sequence, invoking
matches(char)for each character, until this returnsfalseor the end is reached.- Parameters:
sequence- the character sequence to examine, possibly empty- Returns:
trueif this matcher matches every character in the sequence, including when the sequence is empty
-
matchesNoneOf
Returnstrueif 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 returnstrueor the end is reached.- Parameters:
sequence- the character sequence to examine, possibly empty- Returns:
trueif 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-1if 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
-1if no character matches
-
indexIn
Returns the index of the first matching character in a character sequence, starting from a given position, or-1if 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-1if 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
-