Class CharMatcher
- java.lang.Object
-
- com.google.common.base.CharMatcher
-
- All Implemented Interfaces:
Predicate<java.lang.Character>
- Direct Known Subclasses:
BaseEncoding.Alphabet
,CharMatcher.And
,CharMatcher.AnyOf
,CharMatcher.BreakingWhitespace
,CharMatcher.FastMatcher
,CharMatcher.ForPredicate
,CharMatcher.JavaDigit
,CharMatcher.JavaLetter
,CharMatcher.JavaLetterOrDigit
,CharMatcher.JavaLowerCase
,CharMatcher.JavaUpperCase
,CharMatcher.Negated
,CharMatcher.Or
,CharMatcher.RangesMatcher
@GwtCompatible(emulated=true) public abstract class CharMatcher extends java.lang.Object implements Predicate<java.lang.Character>
Determines a true or false value for any Javachar
value, just asPredicate
does for anyObject
. 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
valuec
for whichthis.matches(c)
returnstrue
".Warning: This class deals only with
char
values; it does not understand supplementary Unicode code points in the range0x10000
to0x10FFFF
. Such logical characters are encoded into aString
using surrogate pairs, and aCharMatcher
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
CharMatcher.And
Implementation ofand(CharMatcher)
.private static class
CharMatcher.Any
Implementation ofany()
.private static class
CharMatcher.AnyOf
Implementation ofanyOf(CharSequence)
for three or more characters.private static class
CharMatcher.Ascii
Implementation ofascii()
.private static class
CharMatcher.BitSetMatcher
Fast matcher using aBitSet
table of matching characters.private static class
CharMatcher.BreakingWhitespace
Implementation ofbreakingWhitespace()
.private static class
CharMatcher.Digit
Implementation ofdigit()
.(package private) static class
CharMatcher.FastMatcher
A matcher for which precomputation will not yield any significant benefit.private static class
CharMatcher.ForPredicate
Implementation offorPredicate(Predicate)
.private static class
CharMatcher.InRange
Implementation ofinRange(char, char)
.private static class
CharMatcher.Invisible
Implementation ofinvisible()
.private static class
CharMatcher.Is
Implementation ofis(char)
.private static class
CharMatcher.IsEither
Implementation ofanyOf(CharSequence)
for exactly two characters.private static class
CharMatcher.IsNot
Implementation ofisNot(char)
.private static class
CharMatcher.JavaDigit
Implementation ofjavaDigit()
.private static class
CharMatcher.JavaIsoControl
Implementation ofjavaIsoControl()
.private static class
CharMatcher.JavaLetter
Implementation ofjavaLetter()
.private static class
CharMatcher.JavaLetterOrDigit
Implementation ofjavaLetterOrDigit()
.private static class
CharMatcher.JavaLowerCase
Implementation ofjavaLowerCase()
.private static class
CharMatcher.JavaUpperCase
Implementation ofjavaUpperCase()
.(package private) static class
CharMatcher.NamedFastMatcher
CharMatcher.FastMatcher
which overridestoString()
with a custom name.private static class
CharMatcher.Negated
Implementation ofCharMatcher.Negated.negate()
.(package private) static class
CharMatcher.NegatedFastMatcher
Negation of aCharMatcher.FastMatcher
.private static class
CharMatcher.None
Implementation ofnone()
.private static class
CharMatcher.Or
Implementation ofor(CharMatcher)
.private static class
CharMatcher.RangesMatcher
Implementation that matches characters that fall within multiple ranges.private static class
CharMatcher.SingleWidth
Implementation ofsingleWidth()
.(package private) static class
CharMatcher.Whitespace
Implementation ofwhitespace()
.
-
Field Summary
Fields Modifier and Type Field Description static CharMatcher
ANY
Deprecated.Useany()
instead.static CharMatcher
ASCII
Deprecated.Useascii()
instead.static CharMatcher
BREAKING_WHITESPACE
Deprecated.UsebreakingWhitespace()
instead.static CharMatcher
DIGIT
Deprecated.Usedigit()
instead.private static int
DISTINCT_CHARS
static CharMatcher
INVISIBLE
Deprecated.Useinvisible()
instead.static CharMatcher
JAVA_DIGIT
Deprecated.UsejavaDigit()
instead.static CharMatcher
JAVA_ISO_CONTROL
Deprecated.UsejavaIsoControl()
instead.static CharMatcher
JAVA_LETTER
Deprecated.UsejavaLetter()
instead.static CharMatcher
JAVA_LETTER_OR_DIGIT
Deprecated.UsejavaLetterOrDigit()
instead.static CharMatcher
JAVA_LOWER_CASE
Deprecated.UsejavaLowerCase()
instead.static CharMatcher
JAVA_UPPER_CASE
Deprecated.UsejavaUpperCase()
instead.static CharMatcher
NONE
Deprecated.Usenone()
instead.static CharMatcher
SINGLE_WIDTH
Deprecated.UsesingleWidth()
instead.static CharMatcher
WHITESPACE
Deprecated.Usewhitespace()
instead.
-
Constructor Summary
Constructors Modifier Constructor Description protected
CharMatcher()
Constructor for use by subclasses.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description CharMatcher
and(CharMatcher other)
Returns a matcher that matches any character matched by both this matcher andother
.static CharMatcher
any()
Matches any character.static CharMatcher
anyOf(java.lang.CharSequence sequence)
Returns achar
matcher that matches any character present in the given character sequence.boolean
apply(java.lang.Character character)
Deprecated.Provided only to satisfy thePredicate
interface; usematches(char)
instead.static CharMatcher
ascii()
Determines whether a character is ASCII, meaning that its code point is less than 128.static CharMatcher
breakingWhitespace()
Determines whether a character is a breaking whitespace (that is, a whitespace which can be interpreted as a break between words for formatting purposes).java.lang.String
collapseFrom(java.lang.CharSequence sequence, char replacement)
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.int
countIn(java.lang.CharSequence sequence)
Returns the number of matching characters found in a character sequence.static CharMatcher
digit()
Determines whether a character is a digit according to Unicode.private java.lang.String
finishCollapseFrom(java.lang.CharSequence sequence, int start, int end, char replacement, java.lang.StringBuilder builder, boolean inMatchingGroup)
static CharMatcher
forPredicate(Predicate<? super java.lang.Character> predicate)
Returns a matcher with identical behavior to the givenCharacter
-based predicate, but which operates on primitivechar
instances instead.int
indexIn(java.lang.CharSequence sequence)
Returns the index of the first matching character in a character sequence, or-1
if no matching character is present.int
indexIn(java.lang.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
inRange(char startInclusive, char endInclusive)
Returns achar
matcher that matches any character in a given range (both endpoints are inclusive).static CharMatcher
invisible()
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 CharMatcher
is(char match)
Returns achar
matcher that matches only one specified character.private static CharMatcher.IsEither
isEither(char c1, char c2)
static CharMatcher
isNot(char match)
Returns achar
matcher that matches any character except the one specified.private static boolean
isSmall(int totalCharacters, int tableLength)
static CharMatcher
javaDigit()
Determines whether a character is a digit according to Java's definition.static CharMatcher
javaIsoControl()
Determines whether a character is an ISO control character as specified byCharacter.isISOControl(char)
.static CharMatcher
javaLetter()
Determines whether a character is a letter according to Java's definition.static CharMatcher
javaLetterOrDigit()
Determines whether a character is a letter or digit according to Java's definition.static CharMatcher
javaLowerCase()
Determines whether a character is lower case according to Java's definition.static CharMatcher
javaUpperCase()
Determines whether a character is upper case according to Java's definition.int
lastIndexIn(java.lang.CharSequence sequence)
Returns the index of the last matching character in a character sequence, or-1
if no matching character is present.abstract boolean
matches(char c)
Determines a true or false value for the given character.boolean
matchesAllOf(java.lang.CharSequence sequence)
Returnstrue
if a character sequence contains only matching characters.boolean
matchesAnyOf(java.lang.CharSequence sequence)
Returnstrue
if a character sequence contains at least one matching character.boolean
matchesNoneOf(java.lang.CharSequence sequence)
Returnstrue
if a character sequence contains no matching characters.CharMatcher
negate()
Returns a matcher that matches any character not matched by this matcher.static CharMatcher
none()
Matches no characters.static CharMatcher
noneOf(java.lang.CharSequence sequence)
Returns achar
matcher that matches any character not present in the given character sequence.CharMatcher
or(CharMatcher other)
Returns a matcher that matches any character matched by either this matcher orother
.CharMatcher
precomputed()
Returns achar
matcher functionally equivalent to this one, but which may be faster to query than the original; your mileage may vary.(package private) CharMatcher
precomputedInternal()
This is the actual implementation ofprecomputed()
, but we bounce calls through a method onPlatform
so that we can have different behavior in GWT.private static CharMatcher
precomputedPositive(int totalCharacters, java.util.BitSet table, java.lang.String description)
Helper method forprecomputedInternal()
that doesn't test if the negation is cheaper.java.lang.String
removeFrom(java.lang.CharSequence sequence)
Returns a string containing all non-matching characters of a character sequence, in order.java.lang.String
replaceFrom(java.lang.CharSequence sequence, char replacement)
Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement character.java.lang.String
replaceFrom(java.lang.CharSequence sequence, java.lang.CharSequence replacement)
Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement sequence.java.lang.String
retainFrom(java.lang.CharSequence sequence)
Returns a string containing all matching characters of a character sequence, in order.(package private) void
setBits(java.util.BitSet table)
Sets bits intable
matched by this matcher.private static java.lang.String
showCharacter(char c)
Returns the Java Unicode escape sequence for the given character, in the form "ካ" where "12AB" is the four hexadecimal digits representing the 16 bits of the UTF-16 character.static CharMatcher
singleWidth()
Determines whether a character is single-width (not double-width).java.lang.String
toString()
Returns a string representation of thisCharMatcher
, such asCharMatcher.or(WHITESPACE, JAVA_DIGIT)
.java.lang.String
trimAndCollapseFrom(java.lang.CharSequence sequence, char replacement)
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.java.lang.String
trimFrom(java.lang.CharSequence sequence)
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.java.lang.String
trimLeadingFrom(java.lang.CharSequence sequence)
Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning of the string.java.lang.String
trimTrailingFrom(java.lang.CharSequence sequence)
Returns a substring of the input character sequence that omits all characters this matcher matches from the end of the string.static CharMatcher
whitespace()
Determines whether a character is whitespace according to the latest Unicode standard, as illustrated here.
-
-
-
Field Detail
-
WHITESPACE
@Deprecated public static final CharMatcher WHITESPACE
Deprecated.Usewhitespace()
instead. This constant is scheduled to be removed in June 2018.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.
-
BREAKING_WHITESPACE
@Deprecated public static final CharMatcher BREAKING_WHITESPACE
Deprecated.UsebreakingWhitespace()
instead. This constant is scheduled to be removed in June 2018.Determines whether a character is a breaking whitespace (that is, a whitespace which can be interpreted as a break between words for formatting purposes). Seewhitespace()
for a discussion of that term.- Since:
- 2.0
-
ASCII
@Deprecated public static final CharMatcher ASCII
Deprecated.Useascii()
instead. This constant is scheduled to be removed in June 2018.Determines whether a character is ASCII, meaning that its code point is less than 128.
-
DIGIT
@Deprecated public static final CharMatcher DIGIT
Deprecated.Usedigit()
instead. This constant is scheduled to be removed in June 2018.Determines whether a character is a digit according to Unicode. If you only care to match ASCII digits, you can useinRange('0', '9')
.
-
JAVA_DIGIT
@Deprecated public static final CharMatcher JAVA_DIGIT
Deprecated.UsejavaDigit()
instead. This constant is scheduled to be removed in June 2018.Determines whether a character is a digit according to Java's definition. If you only care to match ASCII digits, you can useinRange('0', '9')
.
-
JAVA_LETTER
@Deprecated public static final CharMatcher JAVA_LETTER
Deprecated.UsejavaLetter()
instead. This constant is scheduled to be removed in June 2018.Determines whether a character is a letter according to Java'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 public static final CharMatcher JAVA_LETTER_OR_DIGIT
Deprecated.UsejavaLetterOrDigit()
instead. This constant is scheduled to be removed in June 2018.Determines whether a character is a letter or digit according to Java's definition.
-
JAVA_UPPER_CASE
@Deprecated public static final CharMatcher JAVA_UPPER_CASE
Deprecated.UsejavaUpperCase()
instead. This constant is scheduled to be removed in June 2018.Determines whether a character is upper case according to Java's definition.
-
JAVA_LOWER_CASE
@Deprecated public static final CharMatcher JAVA_LOWER_CASE
Deprecated.UsejavaLowerCase()
instead. This constant is scheduled to be removed in June 2018.Determines whether a character is lower case according to Java's definition.
-
JAVA_ISO_CONTROL
@Deprecated public static final CharMatcher JAVA_ISO_CONTROL
Deprecated.UsejavaIsoControl()
instead. This constant is scheduled to be removed in June 2018.Determines whether a character is an ISO control character as specified byCharacter.isISOControl(char)
.
-
INVISIBLE
@Deprecated public static final CharMatcher INVISIBLE
Deprecated.Useinvisible()
instead. This constant is scheduled to be removed in June 2018.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 public static final CharMatcher SINGLE_WIDTH
Deprecated.UsesingleWidth()
instead. This constant is scheduled to be removed in June 2018.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 public static final CharMatcher ANY
Deprecated.Useany()
instead. This constant is scheduled to be removed in June 2018.Matches any character.
-
NONE
@Deprecated public static final CharMatcher NONE
Deprecated.Usenone()
instead. This constant is scheduled to be removed in June 2018.Matches no characters.
-
DISTINCT_CHARS
private static final int DISTINCT_CHARS
- See Also:
- Constant Field Values
-
-
Method Detail
-
any
public static CharMatcher any()
Matches any character.- Since:
- 19.0 (since 1.0 as constant
ANY
)
-
none
public static CharMatcher none()
Matches no characters.- Since:
- 19.0 (since 1.0 as constant
NONE
)
-
whitespace
public static CharMatcher whitespace()
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 matcher to keep it up to date.
- Since:
- 19.0 (since 1.0 as constant
WHITESPACE
)
-
breakingWhitespace
public static CharMatcher breakingWhitespace()
Determines whether a character is a breaking whitespace (that is, a whitespace which can be interpreted as a break between words for formatting purposes). Seewhitespace()
for a discussion of that term.- Since:
- 19.0 (since 2.0 as constant
BREAKING_WHITESPACE
)
-
ascii
public static CharMatcher ascii()
Determines whether a character is ASCII, meaning that its code point is less than 128.- Since:
- 19.0 (since 1.0 as constant
ASCII
)
-
digit
public static CharMatcher digit()
Determines whether a character is a digit according to Unicode. If you only care to match ASCII digits, you can useinRange('0', '9')
.- Since:
- 19.0 (since 1.0 as constant
DIGIT
)
-
javaDigit
public static CharMatcher javaDigit()
Determines whether a character is a digit according to Java's definition. If you only care to match ASCII digits, you can useinRange('0', '9')
.- Since:
- 19.0 (since 1.0 as constant
JAVA_DIGIT
)
-
javaLetter
public static CharMatcher javaLetter()
Determines whether a character is a letter according to Java's definition. If you only care to match letters of the Latin alphabet, you can useinRange('a', 'z').or(inRange('A', 'Z'))
.- Since:
- 19.0 (since 1.0 as constant
JAVA_LETTER
)
-
javaLetterOrDigit
public static CharMatcher javaLetterOrDigit()
Determines whether a character is a letter or digit according to Java's definition.- Since:
- 19.0 (since 1.0 as constant
JAVA_LETTER_OR_DIGIT
).
-
javaUpperCase
public static CharMatcher javaUpperCase()
Determines whether a character is upper case according to Java's definition.- Since:
- 19.0 (since 1.0 as constant
JAVA_UPPER_CASE
)
-
javaLowerCase
public static CharMatcher javaLowerCase()
Determines whether a character is lower case according to Java's definition.- Since:
- 19.0 (since 1.0 as constant
JAVA_LOWER_CASE
)
-
javaIsoControl
public static CharMatcher javaIsoControl()
Determines whether a character is an ISO control character as specified byCharacter.isISOControl(char)
.- Since:
- 19.0 (since 1.0 as constant
JAVA_ISO_CONTROL
)
-
invisible
public static CharMatcher invisible()
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.- Since:
- 19.0 (since 1.0 as constant
INVISIBLE
)
-
singleWidth
public static CharMatcher singleWidth()
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 matcher to keep it up to date.
- Since:
- 19.0 (since 1.0 as constant
SINGLE_WIDTH
)
-
is
public static CharMatcher is(char match)
Returns achar
matcher that matches only one specified character.
-
isNot
public static CharMatcher isNot(char match)
Returns achar
matcher that matches any character except the one specified.To negate another
CharMatcher
, usenegate()
.
-
anyOf
public static CharMatcher anyOf(java.lang.CharSequence sequence)
Returns achar
matcher that matches any character present in the given character sequence.
-
noneOf
public static CharMatcher noneOf(java.lang.CharSequence sequence)
Returns achar
matcher that matches any character not present in the given character sequence.
-
inRange
public static CharMatcher inRange(char startInclusive, char endInclusive)
Returns achar
matcher 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:
java.lang.IllegalArgumentException
- ifendInclusive < startInclusive
-
forPredicate
public static CharMatcher forPredicate(Predicate<? super java.lang.Character> predicate)
Returns a matcher with identical behavior to the givenCharacter
-based predicate, but which operates on primitivechar
instances instead.
-
matches
public abstract boolean matches(char c)
Determines a true or false value for the given character.
-
negate
public CharMatcher negate()
Returns a matcher that matches any character not matched by this matcher.
-
and
public CharMatcher and(CharMatcher other)
Returns a matcher that matches any character matched by both this matcher andother
.
-
or
public CharMatcher or(CharMatcher other)
Returns a matcher that matches any character matched by either this matcher orother
.
-
precomputed
public CharMatcher precomputed()
Returns achar
matcher 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.
-
precomputedInternal
@GwtIncompatible CharMatcher precomputedInternal()
This is the actual implementation ofprecomputed()
, but we bounce calls through a method onPlatform
so that we can have different behavior in GWT.This implementation tries to be smart in a number of ways. It recognizes cases where the negation is cheaper to precompute than the matcher itself; it tries to build small hash tables for matchers that only match a few characters, and so on. In the worst-case scenario, it constructs an eight-kilobyte bit array and queries that. In many situations this produces a matcher which is faster to query than the original.
-
precomputedPositive
@GwtIncompatible private static CharMatcher precomputedPositive(int totalCharacters, java.util.BitSet table, java.lang.String description)
Helper method forprecomputedInternal()
that doesn't test if the negation is cheaper.
-
isSmall
@GwtIncompatible private static boolean isSmall(int totalCharacters, int tableLength)
-
setBits
@GwtIncompatible void setBits(java.util.BitSet table)
Sets bits intable
matched by this matcher.
-
matchesAnyOf
public boolean matchesAnyOf(java.lang.CharSequence sequence)
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
public boolean matchesAllOf(java.lang.CharSequence sequence)
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
public boolean matchesNoneOf(java.lang.CharSequence sequence)
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
public int indexIn(java.lang.CharSequence sequence)
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
public int indexIn(java.lang.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.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:
java.lang.IndexOutOfBoundsException
- if start is negative or greater thansequence.length()
-
lastIndexIn
public int lastIndexIn(java.lang.CharSequence sequence)
Returns the index of the last matching character in a character sequence, or-1
if 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
-1
if no character matches
-
countIn
public int countIn(java.lang.CharSequence sequence)
Returns the number of matching characters found in a character sequence.
-
removeFrom
public java.lang.String removeFrom(java.lang.CharSequence sequence)
Returns a string containing all non-matching characters of a character sequence, in order. For example:CharMatcher.is('a').removeFrom("bazaar")
"bzr"
.
-
retainFrom
public java.lang.String retainFrom(java.lang.CharSequence sequence)
Returns a string containing all matching characters of a character sequence, in order. For example:CharMatcher.is('a').retainFrom("bazaar")
"aaa"
.
-
replaceFrom
public java.lang.String replaceFrom(java.lang.CharSequence sequence, char replacement)
Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement character. For example:CharMatcher.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
public java.lang.String replaceFrom(java.lang.CharSequence sequence, java.lang.CharSequence replacement)
Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement sequence. For example:CharMatcher.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
public java.lang.String trimFrom(java.lang.CharSequence sequence)
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:CharMatcher.anyOf("ab").trimFrom("abacatbab")
"cat"
.Note that:
CharMatcher.inRange('\0', ' ').trimFrom(str)
String.trim()
.
-
trimLeadingFrom
public java.lang.String trimLeadingFrom(java.lang.CharSequence sequence)
Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning of the string. For example:CharMatcher.anyOf("ab").trimLeadingFrom("abacatbab")
"catbab"
.
-
trimTrailingFrom
public java.lang.String trimTrailingFrom(java.lang.CharSequence sequence)
Returns a substring of the input character sequence that omits all characters this matcher matches from the end of the string. For example:CharMatcher.anyOf("ab").trimTrailingFrom("abacatbab")
"abacat"
.
-
collapseFrom
public java.lang.String collapseFrom(java.lang.CharSequence sequence, char replacement)
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:CharMatcher.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
public java.lang.String trimAndCollapseFrom(java.lang.CharSequence sequence, char replacement)
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.
-
finishCollapseFrom
private java.lang.String finishCollapseFrom(java.lang.CharSequence sequence, int start, int end, char replacement, java.lang.StringBuilder builder, boolean inMatchingGroup)
-
apply
@Deprecated public boolean apply(java.lang.Character character)
Deprecated.Provided only to satisfy thePredicate
interface; usematches(char)
instead.Description copied from interface:Predicate
Returns the result of applying this predicate toinput
(Java 8 users, see notes in the class documentation above). This method is generally expected, but not absolutely required, to have the following properties:- Its execution does not cause any observable side effects.
- The computation is consistent with equals; that is,
Objects.equal
(a, b)
implies thatpredicate.apply(a) == predicate.apply(b))
.
-
toString
public java.lang.String toString()
Returns a string representation of thisCharMatcher
, such asCharMatcher.or(WHITESPACE, JAVA_DIGIT)
.- Overrides:
toString
in classjava.lang.Object
-
showCharacter
private static java.lang.String showCharacter(char c)
Returns the Java Unicode escape sequence for the given character, in the form "ካ" where "12AB" is the four hexadecimal digits representing the 16 bits of the UTF-16 character.
-
isEither
private static CharMatcher.IsEither isEither(char c1, char c2)
-
-