Monday, November 18, 2024
Google search engine
HomeLanguagesJavaCharMatcher fields with Examples | Guava | Java

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 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:

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 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:

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:




// 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:

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 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:

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 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:

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:




// 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:

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:




// 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:

#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:




// 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:

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

RELATED ARTICLES

Most Popular

Recent Comments