CharMatcher Class | Guava | Java
CharMatcher determines a true or false value for any Java char value. This class provides various methods to handle various Java types for char values.
Declaration: The declaration for com.google.common.base.CharMatcher is as:
@GwtCompatible(emulated = true) public final class CharMatcher extends Object
There are 2 ways to obtain CharMatcher instance:
- Using constants: CharMatcher class provides following constants to obtain CharMatcher instance.

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.
- Using methods provided by CharMatcher class: One can get instance of CharMatcher class by using following methods.
Example 1:
// Java code to get number of matching // characters in given sequence // and display them using countIn() import com.google.common.base.CharMatcher; class GFG { // Driver code public static void main(String args[]) { // "anyOf" method returns a char matcher // that matches any character present in // the given character sequence. CharMatcher matcher = CharMatcher.anyOf("aeiou"); String str = "Hello GeeksforGeeks, What's up ?"; // "countIn" returns the number of matching // characters found in a character sequence. int vowels = matcher.countIn(str); // To display the number of vowels in // character sequence System.out.println("Number of vowels in '" + str + "' are " + vowels); } } |
Number of vowels in 'Hello GeeksforGeeks, What's up ?' are 9
Some other methods of CharMatcher Class are:

Example 2:
// Java code to get the index // of matching character import com.google.common.base.CharMatcher; class GFG { // Driver code public static void main(String args[]) { // "anyOf" method returns a char matcher // that matches any character present in // the given character sequence. CharMatcher matcher = CharMatcher.anyOf("aeiou"); String str = "Hello GeeksforGeeks, What's up ?"; // To return the index of first matching // character in given input sequence. int firstIndex = matcher.indexIn(str); // To Return the index of first matching // character in given input sequence, // from given starting index. int nextIndex = matcher.indexIn(str, firstIndex + 1); // To return the index of the last matching // character in a character sequence int lastIndex = matcher.lastIndexIn(str); System.out.println("First Index is " + firstIndex); System.out.println("Next Index is " + nextIndex); System.out.println("Last Index is " + lastIndex); } } |
First Index is 1 Next Index is 4 Last Index is 28
Some other methods of CharMatcher Class are:

Example 3:
// Java code to remove all digits // from a given string import com.google.common.base.CharMatcher; class GFG { // Driver code public static void main(String args[]) { // Determines whether a character is // a digit according to Unicode. CharMatcher matcher = CharMatcher.DIGIT; String str = "12345Hello GeeksforGeeks1287 What's 9886up"; System.out.println("Original String : " + str); // To remove all matching characters // from given string. String result = matcher.removeFrom(str); // To display the string which // doesn't contain digit System.out.println("After digit removal : " + result); } } |
Original String : 12345Hello GeeksforGeeks1287 What's 9886up After digit removal : Hello GeeksforGeeks What's up
Reference: Google Guava
Recommended Posts:
- CharMatcher fields with Examples | Guava | Java
- Shorts Class | Guava | Java
- Splitter Class | Guava | Java
- Doubles Class | Guava | Java
- LongMath Class | Guava | Java
- CaseFormat Class | Guava | Java
- Joiner class | Guava | Java
- Chars Class | Guava | Java
- Longs Class | Guava | Java
- Bytes Class | Guava | Java
- Ints Class | Guava | Java
- BigIntegerMath Class | Guava | Java
- Optional Class | Guava | Java
- Range Class | Guava | Java
- Booleans Class | Guava | Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

