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:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
String input = "123 is divisible by 3" ;
System.out.println(input);
CharMatcher matcher = CharMatcher.DIGIT;
String result = matcher.retainFrom(input);
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:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
String input = "123 is divisible by 3" ;
System.out.println(input);
CharMatcher matcher = CharMatcher.JAVA_LETTER;
String result = matcher.retainFrom(input);
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:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
String input = "GeekforGeeks is fun.\u00be" ;
System.out.println(input);
CharMatcher matcher = CharMatcher.ASCII;
String result = matcher.retainFrom(input);
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:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
String input = "GeekforGeeks is fun." ;
CharMatcher matcher = CharMatcher.ANY;
String result = matcher.retainFrom(input);
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:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
String input = "gEEKSfORgEEKS" ;
System.out.println(input);
CharMatcher matcher = CharMatcher.JAVA_LOWER_CASE;
String result = matcher.retainFrom(input);
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:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
String input = "c++ JAVA python" ;
System.out.println(input);
CharMatcher matcher = CharMatcher.JAVA_UPPER_CASE;
String result = matcher.retainFrom(input);
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:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
String input = "#13 is a prime & number%" ;
System.out.println(input);
CharMatcher matcher = CharMatcher.JAVA_LETTER_OR_DIGIT;
String result = matcher.retainFrom(input);
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:
import com.google.common.base.CharMatcher;
class GFG {
public static void main(String args[])
{
String input = "13 is a prime number" ;
System.out.println(input);
CharMatcher matcher = CharMatcher.JAVA_DIGIT;
String result = matcher.retainFrom(input);
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
Last Updated :
05 Nov, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...