Guava - CharMatcher Class



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
public abstract class CharMatcher
   extends Object
      implements Predicate<Character>
Sr.No Constructor & Description
1

protected CharMatcher()

Constructor for use by subclasses.

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 any()

Returns a matcher that matches any character.

3

static CharMatcher anyOf(CharSequence sequence)

Returns a char matcher that matches any character present in the given character sequence.

4

boolean apply(Character character)

Deprecated. Provided only to satisfy the Predicate interface; use matches(char) instead.

5

static CharMatcher ascii()

Determines whether a character is ASCII, meaning that its code point is less than 128.

6

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).

7

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.

8

int countIn(CharSequence sequence)

Returns the number of matching characters found in a character sequence.

9

static CharMatcher digit()

Deprecated. Many digits are supplementary characters; see the class documentation.

10

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.

11

int indexIn(CharSequence sequence)

Returns the index of the first matching character in a character sequence, or -1 if no matching character is present.

12

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.

13

static CharMatcher inRange(char startInclusive, char endInclusive)

Returns a char matcher that matches any character in a given range (both endpoints are inclusive).

14

static CharMatcher invisible()

Deprecated. Most invisible characters are supplementary characters; see the class documentation.

15

static CharMatcher is(char match)

Returns a char matcher that matches only one specified character.

16

static CharMatcher isNot(char match)

Returns a char matcher that matches any character except the one specified.

17

static CharMatcher javaDigit()

Deprecated.Many digits are supplementary characters; see the class documentation.

18

static CharMatcher javaIsoControl()

Determines whether a character is an ISO control character as specified by Character.isISOControl(char).

19

static CharMatcher javaLetter()

Deprecated. Most letters are supplementary characters; see the class documentation.

20

static CharMatcher javaLetterOrDigit()

Deprecated. Most letters and digits are supplementary characters; see the class documentation.

21

static CharMatcher javaLowerCase()

Deprecated. Some lowercase characters are supplementary characters; see the class documentation.

22

static CharMatcher javaUpperCase()

Deprecated. Some uppercase characters are supplementary characters; see the class documentation.

23

int lastIndexIn(CharSequence sequence)

Returns the index of the last matching character in a character sequence, or -1 if no matching character is present.

24

abstract boolean matches(char c)

Determines a true or false value for the given character.

25

boolean matchesAllOf(CharSequence sequence)

Returns true if a character sequence contains only matching characters.

26

boolean matchesAnyOf(CharSequence sequence)

Returns true if a character sequence contains at least one matching character.

27

boolean matchesNoneOf(CharSequence sequence)

Returns true if a character sequence contains no matching characters.

28

CharMatcher negate()

Returns a matcher that matches any character not matched by this matcher.

29

static CharMatcher none()

Matches no characters.

30

static CharMatcher noneOf(CharSequence sequence)

Returns a char matcher that matches any character not present in the given character sequence.

31

CharMatcher or(CharMatcher other)

Returns a matcher that matches any character matched by either this matcher or other.

32

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.

33

String removeFrom(CharSequence sequence)

Returns a string containing all non-matching characters of a character sequence, in order.

34

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.

35

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.

36

String retainFrom(CharSequence sequence)

Returns a string containing all matching characters of a character sequence, in order.

37

String toString()

Returns a string representation of this CharMatcher, such as CharMatcher.or(WHITESPACE, JAVA_DIGIT).

38

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.

39

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.

40

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.

41

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 - Retaining only digits from a String

GuavaTester.java

package com.tutorialspoint;

import com.google.common.base.CharMatcher;

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
   }
}

Output

Run the GuavaTester and verify the output −

123

Example - Trim Extra Spaces from a String

GuavaTester.java

package com.tutorialspoint;

import com.google.common.base.CharMatcher;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testCharMatcher();
   }

   private void testCharMatcher() {
      System.out.println(CharMatcher.whitespace().trimAndCollapseFrom("     Mahesh     Parashar ", ' '));
   }
}

Output

Run the GuavaTester and verify the output −

Mahesh Parashar

Example - Replace digits with Special Character

GuavaTester.java

package com.tutorialspoint;

import com.google.common.base.CharMatcher;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testCharMatcher();
   }

   private void testCharMatcher() {
      System.out.println(CharMatcher.javaDigit().replaceFrom("mahesh123", "*"));  // star out all digits
   }
}

Output

Run the GuavaTester and verify the output −

mahesh***
Advertisements