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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

123 is divisible by 3
isdivisibleby

Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_LETTER

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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.

Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_LOWER_CASE

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

c++ JAVA python
JAVA

Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_UPPER_CASE

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:


#13 is a prime & number%
13isaprimenumber

Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_LETTER_OR_DIGIT

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

13 is a prime number
13

Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_DIGIT



My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.