Guava – CharMatcher Class

  • Post author:
  • Post category:Guava
  • Post comments:1 Comment
CharMatcher

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.NoField & Description
1static CharMatcher ANYMatches any character.
2static CharMatcher ASCIIDetermines whether a character is ASCII, meaning that its code point is less than 128.
3static 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).
4static CharMatcher DIGITDetermines whether a character is a digit according to Unicode.
5static 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.
6static CharMatcher JAVA_DIGITDetermines whether a character is a digit according to Java’s definition.
7static CharMatcher JAVA_ISO_CONTROLDetermines whether a character is an ISO control character as specified by Character.isISOControl(char).
8static CharMatcher JAVA_LETTERDetermines whether a character is a letter according to Java’s definition.
9static CharMatcher JAVA_LETTER_OR_DIGITDetermines whether a character is a letter or digit according to Java’s definition.
10static CharMatcher JAVA_LOWER_CASEDetermines whether a character is lower case according to Java’s definition.
11static CharMatcher JAVA_UPPER_CASEDetermines whether a character is upper case according to Java’s definition.
12static CharMatcher NONEMatches no characters.
13static CharMatcher SINGLE_WIDTHDetermines whether a character is single-width (not double-width).
14static CharMatcher WHITESPACEDetermines whether a character is whitespace according to the latest Unicode standard, as illustrated here.

Constructor(s)

Sr.NoConstructor & Description
1protected CharMatcher()Constructor for use by subclasses.

Class Methods

Sr.NoMethods & Description
1CharMatcher and(CharMatcher other)Returns a matcher that matches any character matched by both this matcher and other.
2static CharMatcher anyOf(CharSequence sequence)Returns a char matcher that matches any character present in the given character sequence.
3boolean apply(Character character)Deprecated. Provided only to satisfy the Predicate interface; use matches(char) instead.
4String 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.
5int countIn(CharSequence sequence)Returns the number of matching characters found in a character sequence.
6static 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.
7int indexIn(CharSequence sequence)Returns the index of the first matching character in a character sequence, or -1 if no matching character is present.
8int 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.
9static CharMatcher inRange(char startInclusive, char endInclusive)Returns a char matcher that matches any character in a given range (both endpoints are inclusive).
10static CharMatcher is(char match)Returns a char matcher that matches only one specified character.
11static CharMatcher isNot(char match)Returns a char matcher that matches any character except the one specified.
12int lastIndexIn(CharSequence sequence)Returns the index of the last matching character in a character sequence, or -1 if no matching character is present.
13abstract boolean matches(char c)Determines a true or false value for the given character.
14boolean matchesAllOf(CharSequence sequence)Returns true if a character sequence contains only matching characters.
15boolean matchesAnyOf(CharSequence sequence)Returns true if a character sequence contains at least one matching character.
16boolean matchesNoneOf(CharSequence sequence)Returns true if a character sequence contains no matching characters.
17CharMatcher negate()Returns a matcher that matches any character not matched by this matcher.
18static CharMatcher noneOf(CharSequence sequence)Returns a char matcher that matches any character not present in the given character sequence.
19CharMatcher or(CharMatcher other)Returns a matcher that matches any character matched by either this matcher or other.
20CharMatcher precomputed()Returns a char matcher functionally equivalent to this one, but which may be faster to query than the original; your mileage may vary.
21String removeFrom(CharSequence sequence)Returns a string containing all non-matching characters of a character sequence, in order.
22String 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.
23String 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.
24String retainFrom(CharSequence sequence)Returns a string containing all matching characters of a character sequence, in order.
25String toString()Returns a string representation of this CharMatcher, such as CharMatcher.or(WHITESPACE, JAVA_DIGIT).
26String 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.
27String 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.
28String trimLeadingFrom(CharSequence sequence)Returns a substring of the input character sequence that omits all characters this matcher matches from the beginning of the string.
29String 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

This Post Has One Comment

Leave a Reply