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:
Example 1:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
CharMatcher matcher = CharMatcher.anyOf( "aeiou" );
String str = "Hello GeeksforGeeks, What's up ?" ;
int vowels = matcher.countIn(str);
System.out.println( "Number of vowels in '"
+ str + "' are "
+ vowels);
}
}
|
Output:
Number of vowels in 'Hello GeeksforGeeks, What's up ?' are 9
Some other methods of CharMatcher Class are:
Example 2:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
CharMatcher matcher = CharMatcher.anyOf( "aeiou" );
String str = "Hello GeeksforGeeks, What's up ?" ;
int firstIndex = matcher.indexIn(str);
int nextIndex = matcher.indexIn(str, firstIndex + 1 );
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);
}
}
|
Output:
First Index is 1
Next Index is 4
Last Index is 28
Some other methods of CharMatcher Class are:
Example 3:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
CharMatcher matcher = CharMatcher.DIGIT;
String str = "12345Hello GeeksforGeeks1287 What's 9886up" ;
System.out.println( "Original String : " + str);
String result = matcher.removeFrom(str);
System.out.println( "After digit removal : " + result);
}
}
|
Output:
Original String : 12345Hello GeeksforGeeks1287 What's 9886up
After digit removal : Hello GeeksforGeeks What's up
Reference: Google Guava
Last Updated :
15 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...