CharMatcher provides various methods to handle various JAVA types for char values.
Class Declaration
Following is the declaration for com.google.common.base.CharMatcher class −
@GwtCompatible(emulated = true) public final class CharMatcher extends Object
Fields
Sr.No | Field & Description |
---|---|
1 | static CharMatcher ANYMatches any character. |
2 | static CharMatcher ASCIIDetermines whether a character is ASCII, meaning that its code point is less than 128. |
3 | static CharMatcher BREAKING_WHITESPACEDetermines whether a character is a breaking whitespace (that is, a whitespace which can be interpreted as a break between words for formatting purposes). |
4 | static CharMatcher DIGITDetermines whether a character is a digit according to Unicode. |
5 | static CharMatcher INVISIBLEDetermines 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. |
6 | static CharMatcher JAVA_DIGITDetermines whether a character is a digit according to Java’s definition. |
7 | static CharMatcher JAVA_ISO_CONTROLDetermines whether a character is an ISO control character as specified by Character.isISOControl(char). |
8 | static CharMatcher JAVA_LETTERDetermines whether a character is a letter according to Java’s definition. |
9 | static CharMatcher JAVA_LETTER_OR_DIGITDetermines whether a character is a letter or digit according to Java’s definition. |
10 | static CharMatcher JAVA_LOWER_CASEDetermines whether a character is lower case according to Java’s definition. |
11 | static CharMatcher JAVA_UPPER_CASEDetermines whether a character is upper case according to Java’s definition. |
12 | static CharMatcher NONEMatches no characters. |
13 | static CharMatcher SINGLE_WIDTHDetermines whether a character is single-width (not double-width). |
14 | static CharMatcher WHITESPACEDetermines whether a character is whitespace according to the latest Unicode standard, as illustrated here. |
Constructor(s)
Sr.No | Constructor & Description |
---|---|
1 | protected CharMatcher()Constructor for use by subclasses. |
Class Methods
Sr.No | Methods & Description |
---|---|
1 | CharMatcher and(CharMatcher other)Returns a matcher that matches any character matched by both this matcher and other. |
2 | static CharMatcher anyOf(CharSequence sequence)Returns a char matcher that matches any character present in the given character sequence. |
3 | boolean apply(Character character)Deprecated. Provided only to satisfy the Predicate interface; use matches(char) instead. |
4 | String collapseFrom(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. |
5 | int countIn(CharSequence sequence)Returns the number of matching characters found in a character sequence. |
6 | static CharMatcher forPredicate(Predicate<? super Character> predicate)Returns a matcher with identical behavior to the given Character-based predicate, but which operates on primitive char instances instead. |
7 | int indexIn(CharSequence sequence)Returns the index of the first matching character in a character sequence, or -1 if no matching character is present. |
8 | 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. |
9 | static CharMatcher inRange(char startInclusive, char endInclusive)Returns a char matcher that matches any character in a given range (both endpoints are inclusive). |
10 | static CharMatcher is(char match)Returns a char matcher that matches only one specified character. |
11 | static CharMatcher isNot(char match)Returns a char matcher that matches any character except the one specified. |
12 | int lastIndexIn(CharSequence sequence)Returns the index of the last matching character in a character sequence, or -1 if no matching character is present. |
13 | abstract boolean matches(char c)Determines a true or false value for the given character. |
14 | boolean matchesAllOf(CharSequence sequence)Returns true if a character sequence contains only matching characters. |
15 | boolean matchesAnyOf(CharSequence sequence)Returns true if a character sequence contains at least one matching character. |
16 | boolean matchesNoneOf(CharSequence sequence)Returns true if a character sequence contains no matching characters. |
17 | CharMatcher negate()Returns a matcher that matches any character not matched by this matcher. |
18 | static CharMatcher noneOf(CharSequence sequence)Returns a char matcher that matches any character not present in the given character sequence. |
19 | CharMatcher or(CharMatcher other)Returns a matcher that matches any character matched by either this matcher or other. |
20 | CharMatcher precomputed()Returns a char matcher functionally equivalent to this one, but which may be faster to query than the original; your mileage may vary. |
21 | String removeFrom(CharSequence sequence)Returns a string containing all non-matching characters of a character sequence, in order. |
22 | String replaceFrom(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. |
23 | String replaceFrom(CharSequence sequence, CharSequence replacement)Returns a string copy of the input character sequence, with each character that matches this matcher replaced by a given replacement sequence. |
24 | String retainFrom(CharSequence sequence)Returns a string containing all matching characters of a character sequence, in order. |
25 | String toString()Returns a string representation of this CharMatcher, such as CharMatcher.or(WHITESPACE, JAVA_DIGIT). |
26 | String trimAndCollapseFrom(CharSequence sequence, char replacement)Collapses groups of matching characters exactly as collapseFrom(java.lang.CharSequence, char) does, except that groups of matching characters at the start or end of the sequence are removed without replacement. |
27 | String trimFrom(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. |
28 | String trimLeadingFrom(CharSequence sequence)Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning of the string. |
29 | String trimTrailingFrom(CharSequence sequence)Returns a substring of the input character sequence that omits all characters this matcher matches from the end of the string. |
Methods Inherited
This class inherits methods from the following classes −
- java.lang.Object
Example of CharMatcher class
Create the following java program using any editor of your choice in say C:/> Guava.
GuavaTester.java
import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; public class GuavaTester { public static void main(String args[]) { GuavaTester tester = new GuavaTester(); tester.testCharMatcher(); } private void testCharMatcher() { System.out.println(CharMatcher.DIGIT.retainFrom("mahesh123")); // only the digits System.out.println(CharMatcher.WHITESPACE.trimAndCollapseFrom(" Mahesh Parashar ", ' ')); // trim whitespace at ends, and replace/collapse whitespace into single spaces System.out.println(CharMatcher.JAVA_DIGIT.replaceFrom("mahesh123", "*")); // star out all digits System.out.println(CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_LOWER_CASE).retainFrom("mahesh123")); // eliminate all characters that aren't digits or lowercase } }
Verify the Result
Compile the class using javac compiler as follows −
C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.
C:\Guava>java GuavaTester
See the result.
123 Mahesh Parashar mahesh*** mahesh123
Previous Page:-Click Here
I think this is a real great post.Thanks Again. Keep writing.