CharMatcher class provides the following constants to obtain CharMatcher instance.
Below are some of them
Java
Java
Java
Java
Java
Java
Java
Java
Below are some of them
DIGIT
CharMatcher.DIGIT determines whether a character is a digit according to Unicode. If you only care to match ASCII digits, you can use inRange('0', '9'). Syntax:public static final CharMatcher DIGITBelow is the implementation of the above field. Program 1:
// Program for CharMatcher.DIGIT field in Java
import com.google.common.base.CharMatcher;
class GFG {
// Driver code
public static void main(String args[])
{
// Input string to check for matching
String input = "123 is divisible by 3";
// Printing the input
System.out.println(input);
// Declaring a CharMatcher object
CharMatcher matcher = CharMatcher.DIGIT;
// Retaining the result after matching
String result = matcher.retainFrom(input);
// Printing the result
System.out.println(result);
}
}
Output:
Reference: https://guava.dev/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#DIGIT
123 is divisible by 3 1233
JAVA_LETTER
CharMatcher.JAVA_LETTER determines whether a character is a letter or digit according to Java's definition. Syntax:public static final CharMatcher JAVA_LETTERBelow is the implementation of the above field. Program 1:
// Program for CharMatcher.JAVA_LETTER field in Java
import com.google.common.base.CharMatcher;
class GFG {
// Driver code
public static void main(String args[])
{
// Input string to check for matching
String input = "123 is divisible by 3";
// Printing the input
System.out.println(input);
// Declaring a CharMatcher object
CharMatcher matcher = CharMatcher.JAVA_LETTER;
// Retaining the result after matching
String result = matcher.retainFrom(input);
// Printing the result
System.out.println(result);
}
}
Output:
Reference: https://guava.dev/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_LETTER
123 is divisible by 3 isdivisibleby
ASCII
CharMatcher.ASCII determines whether a character is ASCII, meaning that its code point is less than 128. Syntax:public static final CharMatcher ASCIIBelow is the implementation of the above field. Program 1:
// Program for CharMatcher.ASCII field in Java
import com.google.common.base.CharMatcher;
class GFG {
// Driver code
public static void main(String args[])
{
// Input string to check for matching
String input = "GeekforGeeks is fun.\u00be";
// Printing the input
System.out.println(input);
// Declaring a CharMatcher object
CharMatcher matcher = CharMatcher.ASCII;
// Retaining the result after matching
String result = matcher.retainFrom(input);
// Printing the result
System.out.println(result);
}
}
Output:
Reference: https://guava.dev/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#ASCII
GeekforGeeks is fun.? GeekforGeeks is fun.
ANY
CharMatcher.ANY field matches any character i.e. it matches all the characters. Syntax:public static final CharMatcher ANYBelow is the implementation of the above field. Program 1:
// Program for CharMatcher.ANY field in Java
import com.google.common.base.CharMatcher;
class GFG {
// Driver code
public static void main(String args[])
{
// Input string to check for matching
String input = "GeekforGeeks is fun.";
// Declaring a CharMatcher object
CharMatcher matcher = CharMatcher.ANY;
// Retaining the result after matching
String result = matcher.retainFrom(input);
// Printing the result
System.out.println(result);
}
}
Output:
Reference: https://guava.dev/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#ANY
GeekforGeeks is fun.
JAVA_LOWER_CASE
CharMatcher.JAVA_LOWER_CASE determines whether a character is lower case according to Java's definition. Syntax:public static final CharMatcher JAVA_LOWER_CASEBelow is the implementation of the above field. Program 1:
// Program for CharMatcher.JAVA_LETTER_OR_DIGIT field in Java
import com.google.common.base.CharMatcher;
class GFG {
// Driver code
public static void main(String args[])
{
// Input string to check for matching
String input = "gEEKSfORgEEKS";
// Printing the input
System.out.println(input);
// Declaring a CharMatcher object
CharMatcher matcher = CharMatcher.JAVA_LOWER_CASE;
// Retaining the result after matching
String result = matcher.retainFrom(input);
// Printing the result
System.out.println(result);
}
}
Output:
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.
Reference: https://guava.dev/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_LOWER_CASE
gEEKSfORgEEKS gfg
JAVA_UPPER_CASE
CharMatcher.JAVA_UPPER_CASE determines whether a character is upper case according to Java's definition. Syntax:public static final CharMatcher JAVA_UPPER_CASEBelow is the implementation of the above field. Program 1:
// Program for CharMatcher.JAVA_UPPER_CASE field in Java
import com.google.common.base.CharMatcher;
class GFG {
// Driver code
public static void main(String args[])
{
// Input string to check for matching
String input = "c++ JAVA python";
// Printing the input
System.out.println(input);
// Declaring a CharMatcher object
CharMatcher matcher = CharMatcher.JAVA_UPPER_CASE;
// Retaining the result after matching
String result = matcher.retainFrom(input);
// Printing the result
System.out.println(result);
}
}
Output:
Reference: https://guava.dev/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_UPPER_CASE
c++ JAVA python JAVA
JAVA_LETTER_OR_DIGIT
CharMatcher.JAVA_LETTER_OR_DIGIT determines whether a character is a letter or digit according to Java's definition. Syntax:public static final CharMatcher JAVA_LETTER_OR_DIGITBelow is the implementation of the above field. Program 1:
// Program for CharMatcher.JAVA_LETTER_OR_DIGIT field in Java
import com.google.common.base.CharMatcher;
class GFG {
// Driver code
public static void main(String args[])
{
// Input string to check for matching
String input = "#13 is a prime & number%";
// Printing the input
System.out.println(input);
// Declaring a CharMatcher object
CharMatcher matcher = CharMatcher.JAVA_LETTER_OR_DIGIT;
// Retaining the result after matching
String result = matcher.retainFrom(input);
// Printing the result
System.out.println(result);
}
}
Output:
Reference: https://guava.dev/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_LETTER_OR_DIGIT
#13 is a prime & number% 13isaprimenumber
JAVA_DIGIT
CharMatcher.JAVA_DIGIT determines whether a character is a digit according to Java's definition. If you only care to match ASCII digits, you can use inRange('0', '9'). Syntax:public static final CharMatcher JAVA_DIGITBelow is the implementation of the above field. Program 1:
// Program for CharMatcher.JAVA_DIGIT field in Java
import com.google.common.base.CharMatcher;
class GFG {
// Driver code
public static void main(String args[])
{
// Input string to check for matching
String input = "13 is a prime number";
// Printing the input
System.out.println(input);
// Declaring a CharMatcher object
CharMatcher matcher = CharMatcher.JAVA_DIGIT;
// Retaining the result after matching
String result = matcher.retainFrom(input);
// Printing the result
System.out.println(result);
}
}
Output:
Reference: https://guava.dev/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_DIGIT13 is a prime number 13