Class CharMatcher
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 character c for which this.matches(c) returns true".
Note: 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.
Example usages:
String trimmed = WHITESPACE.trimFrom(userInput);
if (ASCII.matchesAllOf(s)) { ... }
See the Guava User Guide article on
CharMatcher.
- Since:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CharMatcherDeprecated.Matches any character.static final CharMatcherDeprecated.Determines whether a character is ASCII, meaning that its code point is less than 128.static final CharMatcherDeprecated.Determines whether a character is a breaking whitespace (that is, a whitespace which can be interpreted as a break between words for formatting purposes).static final CharMatcherDeprecated.Determines whether a character is a digit according to Unicode.static final CharMatcherDeprecated.Determines whether a character is invisible; that is, if its Unicode category is any of SPACE_SEPARATOR, LINE_SEPARATOR, PARAGRAPH_SEPARATOR, CONTROL, FORMAT, SURROGATE, and PRIVATE_USE according to ICU4J.static final CharMatcherDeprecated.Determines whether a character is a digit according toJava's definition.static final CharMatcherDeprecated.Determines whether a character is an ISO control character as specified byCharacter.isISOControl(char).static final CharMatcherDeprecated.Determines whether a character is a letter according toJava's definition.static final CharMatcherDeprecated.Determines whether a character is a letter or digit according toJava's definition.static final CharMatcherDeprecated.Determines whether a character is lower case according toJava's definition.static final CharMatcherDeprecated.Determines whether a character is upper case according toJava's definition.static final CharMatcherDeprecated.Matches no characters.static final CharMatcherDeprecated.Determines whether a character is single-width (not double-width).static final CharMatcherDeprecated.Determines whether a character is whitespace according to the latest Unicode standard, as illustrated here. -
Method Summary
Modifier and TypeMethodDescriptionand(CharMatcher other) Deprecated.Returns a matcher that matches any character matched by both this matcher andother.static CharMatcheranyOf(CharSequence sequence) Deprecated.Returns acharmatcher that matches any character present in the given character sequence.booleanDeprecated.Equivalent tomatches(char); provided only to satisfy thePredicateinterface.collapseFrom(CharSequence sequence, char replacement) Deprecated.Returns a string copy of the input character sequence, with each group of consecutive characters that match this matcher replaced by a single replacement character.intcountIn(CharSequence sequence) Deprecated.Returns the number of matching characters found in a character sequence.static CharMatcherforPredicate(Predicate<? super Character> predicate) Deprecated.Returns a matcher with identical behavior to the givenCharacter-based predicate, but which operates on primitivecharinstances instead.intindexIn(CharSequence sequence) Deprecated.Returns the index of the first matching character in a character sequence, or-1if no matching character is present.intindexIn(CharSequence sequence, int start) Deprecated.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 CharMatcherinRange(char startInclusive, char endInclusive) Deprecated.Returns acharmatcher that matches any character in a given range (both endpoints are inclusive).static CharMatcheris(char match) Deprecated.Returns acharmatcher that matches only one specified character.static CharMatcherisNot(char match) Deprecated.Returns acharmatcher that matches any character except the one specified.intlastIndexIn(CharSequence sequence) Deprecated.Returns the index of the last matching character in a character sequence, or-1if no matching character is present.abstract booleanmatches(char c) Deprecated.Determines a true or false value for the given character.booleanmatchesAllOf(CharSequence sequence) Deprecated.Returnstrueif a character sequence contains only matching characters.booleanmatchesAnyOf(CharSequence sequence) Deprecated.Returnstrueif a character sequence contains at least one matching character.booleanmatchesNoneOf(CharSequence sequence) Deprecated.Returnstrueif a character sequence contains no matching characters.negate()Deprecated.Returns a matcher that matches any character not matched by this matcher.static CharMatchernoneOf(CharSequence sequence) Deprecated.Returns acharmatcher that matches any character not present in the given character sequence.or(CharMatcher other) Deprecated.Returns a matcher that matches any character matched by either this matcher orother.Deprecated.Returns acharmatcher functionally equivalent to this one, but which may be faster to query than the original; your mileage may vary.removeFrom(CharSequence sequence) Deprecated.Returns a string containing all non-matching characters of a character sequence, in order.replaceFrom(CharSequence sequence, char replacement) Deprecated.Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement character.replaceFrom(CharSequence sequence, CharSequence replacement) Deprecated.Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement sequence.retainFrom(CharSequence sequence) Deprecated.Returns a string containing all matching characters of a character sequence, in order.toString()Deprecated.Returns a string representation of thisCharMatcher, such asCharMatcher.or(WHITESPACE, JAVA_DIGIT).trimAndCollapseFrom(CharSequence sequence, char replacement) Deprecated.Collapses groups of matching characters exactly ascollapseFrom(java.lang.CharSequence, char)does, except that groups of matching characters at the start or end of the sequence are removed without replacement.trimFrom(CharSequence sequence) Deprecated.Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning and from the end of the string.trimLeadingFrom(CharSequence sequence) Deprecated.Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning of the string.trimTrailingFrom(CharSequence sequence) Deprecated.Returns a substring of the input character sequence that omits all characters this matcher matches from the end of the string.
-
Field Details
-
BREAKING_WHITESPACE
Deprecated.Determines whether a character is a breaking whitespace (that is, a whitespace which can be interpreted as a break between words for formatting purposes). SeeWHITESPACEfor a discussion of that term.- Since:
- 2.0
-
ASCII
Deprecated.Determines whether a character is ASCII, meaning that its code point is less than 128. -
DIGIT
Deprecated.Determines whether a character is a digit according to Unicode. -
JAVA_DIGIT
Deprecated.Determines whether a character is a digit according toJava's definition. If you only care to match ASCII digits, you can useinRange('0', '9'). -
JAVA_LETTER
Deprecated.Determines whether a character is a letter according toJava's definition. If you only care to match letters of the Latin alphabet, you can useinRange('a', 'z').or(inRange('A', 'Z')). -
JAVA_LETTER_OR_DIGIT
Deprecated.Determines whether a character is a letter or digit according toJava's definition. -
JAVA_UPPER_CASE
Deprecated.Determines whether a character is upper case according toJava's definition. -
JAVA_LOWER_CASE
Deprecated.Determines whether a character is lower case according toJava's definition. -
JAVA_ISO_CONTROL
Deprecated.Determines whether a character is an ISO control character as specified byCharacter.isISOControl(char). -
INVISIBLE
Deprecated.Determines whether a character is invisible; that is, if its Unicode category is any of SPACE_SEPARATOR, LINE_SEPARATOR, PARAGRAPH_SEPARATOR, CONTROL, FORMAT, SURROGATE, and PRIVATE_USE according to ICU4J. -
SINGLE_WIDTH
Deprecated.Determines whether a character is single-width (not double-width). When in doubt, this matcher errs on the side of returningfalse(that is, it tends to assume a character is double-width).Note: as the reference file evolves, we will modify this constant to keep it up to date.
-
ANY
Deprecated.Matches any character. -
NONE
Deprecated.Matches no characters. -
WHITESPACE
Deprecated.Determines whether a character is whitespace according to the latest Unicode standard, as illustrated here. This is not the same definition used by other Java APIs. (See a comparison of several definitions of "whitespace".)Note: as the Unicode definition evolves, we will modify this constant to keep it up to date.
-
-
Method Details
-
is
Deprecated.Returns acharmatcher that matches only one specified character. -
isNot
Deprecated.Returns acharmatcher that matches any character except the one specified.To negate another
CharMatcher, usenegate(). -
anyOf
Deprecated.Returns acharmatcher that matches any character present in the given character sequence. -
noneOf
Deprecated.Returns acharmatcher that matches any character not present in the given character sequence. -
inRange
Deprecated.Returns acharmatcher that matches any character in a given range (both endpoints are inclusive). For example, to match any lowercase letter of the English alphabet, useCharMatcher.inRange('a', 'z').- Throws:
IllegalArgumentException- ifendInclusive < startInclusive
-
forPredicate
Deprecated.Returns a matcher with identical behavior to the givenCharacter-based predicate, but which operates on primitivecharinstances instead. -
matches
public abstract boolean matches(char c) Deprecated.Determines a true or false value for the given character. -
negate
Deprecated.Returns a matcher that matches any character not matched by this matcher. -
and
Deprecated.Returns a matcher that matches any character matched by both this matcher andother. -
or
Deprecated.Returns a matcher that matches any character matched by either this matcher orother. -
precomputed
Deprecated.Returns acharmatcher functionally equivalent to this one, but which may be faster to query than the original; your mileage may vary. Precomputation takes time and is likely to be worthwhile only if the precomputed matcher is queried many thousands of times.This method has no effect (returns
this) when called in GWT: it's unclear whether a precomputed matcher is faster, but it certainly consumes more memory, which doesn't seem like a worthwhile tradeoff in a browser. -
matchesAnyOf
Deprecated.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
Deprecated.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
Deprecated.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 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
-
indexIn
Deprecated.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
Deprecated.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()
-
lastIndexIn
Deprecated.Returns the index of the last matching character in a character sequence, or-1if no matching character is present.The default implementation iterates over the sequence in reverse order calling
matches(char)for each character.- Parameters:
sequence- the character sequence to examine from the end- Returns:
- an index, or
-1if no character matches
-
countIn
Deprecated.Returns the number of matching characters found in a character sequence. -
removeFrom
Deprecated.Returns a string containing all non-matching characters of a character sequence, in order. For example:
... returnsCharMatcher.is('a').removeFrom("bazaar")"bzr". -
retainFrom
Deprecated.Returns a string containing all matching characters of a character sequence, in order. For example:
... returnsCharMatcher.is('a').retainFrom("bazaar")"aaa". -
replaceFrom
Deprecated.Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement character. For example:
... returnsCharMatcher.is('a').replaceFrom("radar", 'o')"rodor".The default implementation uses
indexIn(CharSequence)to find the first matching character, then iterates the remainder of the sequence callingmatches(char)for each character.- Parameters:
sequence- the character sequence to replace matching characters inreplacement- the character to append to the result string in place of each matching character insequence- Returns:
- the new string
-
replaceFrom
Deprecated.Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement sequence. For example:
... returnsCharMatcher.is('a').replaceFrom("yaha", "oo")"yoohoo".Note: If the replacement is a fixed string with only one character, you are better off calling
replaceFrom(CharSequence, char)directly.- Parameters:
sequence- the character sequence to replace matching characters inreplacement- the characters to append to the result string in place of each matching character insequence- Returns:
- the new string
-
trimFrom
Deprecated.Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning and from the end of the string. For example:
... returnsCharMatcher.anyOf("ab").trimFrom("abacatbab")"cat".Note that:
... is equivalent toCharMatcher.inRange('\0', ' ').trimFrom(str)String.trim(). -
trimLeadingFrom
Deprecated.Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning of the string. For example:
... returnsCharMatcher.anyOf("ab").trimLeadingFrom("abacatbab")"catbab". -
trimTrailingFrom
Deprecated.Returns a substring of the input character sequence that omits all characters this matcher matches from the end of the string. For example:
... returnsCharMatcher.anyOf("ab").trimTrailingFrom("abacatbab")"abacat". -
collapseFrom
Deprecated.Returns a string copy of the input character sequence, with each group of consecutive characters that match this matcher replaced by a single replacement character. For example:
... returnsCharMatcher.anyOf("eko").collapseFrom("bookkeeper", '-')"b-p-r".The default implementation uses
indexIn(CharSequence)to find the first matching character, then iterates the remainder of the sequence callingmatches(char)for each character.- Parameters:
sequence- the character sequence to replace matching groups of characters inreplacement- the character to append to the result string in place of each group of matching characters insequence- Returns:
- the new string
-
trimAndCollapseFrom
Deprecated.Collapses groups of matching characters exactly ascollapseFrom(java.lang.CharSequence, char)does, except that groups of matching characters at the start or end of the sequence are removed without replacement. -
apply
Deprecated.Equivalent tomatches(char); provided only to satisfy thePredicateinterface. When using a reference of typeCharMatcher, invokematches(char)directly instead. -
toString
Deprecated.Returns a string representation of thisCharMatcher, such asCharMatcher.or(WHITESPACE, JAVA_DIGIT).
-