CharMatcher fields with Examples | Guava | Java
CharMatcher class provides the following constants to obtain CharMatcher instance.
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 DIGIT
Below is the implementation of the above field.
Program 1:
// Program for CharMatcher.DIGIT feild 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); } } |
123 is divisible by 3 1233
Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#DIGIT
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_LETTER
Below is the implementation of the above field.
Program 1:
// Program for CharMatcher.JAVA_LETTER feild 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); } } |
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 ASCII
Below is the implementation of the above field.
Program 1:
// Program for CharMatcher.ASCII feild 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); } } |
GeekforGeeks is fun.? GeekforGeeks is fun.
Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#ASCII
ANY
CharMatcher.ANY field matches any character i.e. it matches all the characters.
Syntax:
public static final CharMatcher ANY
Below is the implementation of the above field.
Program 1:
// Program for CharMatcher.ANY feild 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); } } |
GeekforGeeks is fun.
Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#ANY
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_CASE
Below is the implementation of the above field.
Program 1:
// Program for CharMatcher.JAVA_LETTER_OR_DIGIT feild 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); } } |
gEEKSfORgEEKS gfg
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.
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_CASE
Below is the implementation of the above field.
Program 1:
// Program for CharMatcher.JAVA_UPPER_CASE feild 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); } } |
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_DIGIT
Below is the implementation of the above field.
Program 1:
// Program for CharMatcher.JAVA_LETTER_OR_DIGIT feild 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); } } |
#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_DIGIT
Below is the implementation of the above field.
Program 1:
// Program for CharMatcher.JAVA_DIGIT feild 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); } } |
13 is a prime number 13
Recommended Posts:
- CharMatcher Class | Guava | Java
- Java Guava | Shorts.contains() method with Examples
- Java Guava | Longs.min() method with Examples
- Java Guava | Chars.min() method with Examples
- Java Guava | Floats.min() method with Examples
- Java Guava | Longs.max() method with Examples
- Java Guava | Doubles.max() method with Examples
- Java Guava | Booleans.contains() method with Examples
- Java Guava | Shorts.max() method with Examples
- Java Guava | Longs.contains() method with Examples
- Java Guava | Chars.max() method with Examples
- Java Guava | Chars.contains() method with Examples
- Java Guava | Doubles.contains() method with Examples
- Java Guava | Shorts.min() method with Examples
- Java Guava | Floats.contains() method with Examples
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.





