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 Lazyroar, 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 Lazyroar, 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 Lazyroar, 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 Lazyroar1287 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 Lazyroar1287 What's 9886up After digit removal : Hello Lazyroar What's up
Reference: Google Guava