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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Number of vowels in 'Hello GeeksforGeeks, What's up ?' are 9

Some other methods of CharMatcher Class are:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

First Index is 1
Next Index is 4
Last Index is 28

Some other methods of CharMatcher Class are:

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Original String : 12345Hello GeeksforGeeks1287 What's 9886up
After digit removal : Hello GeeksforGeeks What's up

Reference: Google Guava



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.