Java CharMatcher字段与实例
CharMatcher类提供了以下常量来获得CharMatcher实例。
下面是其中的一些
数字
CharMatcher.DIGIT 根据Unicode确定一个字符是否是一个数字。如果你只关心匹配ASCII数字,你可以使用inRange(‘0’, ‘9’ ) 。
语法
public static final CharMatcher DIGIT
以下是上述领域的实现情况。
程序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);
}
}
输出:
123 is divisible by 3
1233
参考资料: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#DIGIT
JAVA_LETTER
CharMatcher.JAVA_LETTER 根据Java的定义来确定一个字符是字母还是数字。
语法
public static final CharMatcher JAVA_LETTER
以下是上述领域的实现情况。
程序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);
}
}
输出:
123 is divisible by 3
isdivisibleby
参考资料: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_LETTER
ASCII
CharMatcher.ASCII 确定一个字符是否为ASCII,即其码位小于128。
语法:
public static final CharMatcher ASCII
以下是上述领域的实现情况。
程序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);
}
}
输出:
GeekforGeeks is fun.?
GeekforGeeks is fun.
参考资料: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#ASCII
ANY
CharMatcher.ANY 字段匹配任何字符,即匹配所有字符。
语法
public static final CharMatcher ANY
以下是上述领域的实现情况。
程序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);
}
}
输出:
GeekforGeeks is fun.
参考资料: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#ANY
java_lower_case
CharMatcher.JAVA_LOWER_CASE 根据Java的定义来确定一个字符是否为小写。
语法:
public static final CharMatcher JAVA_LOWER_CASE
以下是上述领域的实现情况。
程序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);
}
}
输出:
gEEKSfORgEEKS
gfg
注意: 这个类只处理char值。它不理解0x10000到0x10FFFF范围内的补充Unicode代码点。这样的逻辑字符是用代理对编码成一个字符串的,CharMatcher只是把它们当作两个独立的字符。
参考 :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 根据Java的定义来决定一个字符是否是大写字母。
语法:
public static final CharMatcher JAVA_UPPER_CASE
以下是上述领域的实现情况。
程序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);
}
}
输出:
c++ JAVA python
JAVA
参考资料 :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 根据Java的定义来确定一个字符是字母还是数字。
语法:
public static final CharMatcher JAVA_LETTER_OR_DIGIT
以下是上述领域的实现情况。
程序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);
}
}
输出:
#13 is a prime & number%
13isaprimenumber
参考资料: 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 根据Java的定义来确定一个字符是否为数字。如果只关注ASCII数字的匹配,可以使用inRange(‘0’, ‘9’ ) 。
语法:
public static final CharMatcher JAVA_DIGIT
以下是上述领域的实现情况。
程序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);
}
}
输出:
13 is a prime number
13
参考资料: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/CharMatcher.html#JAVA_DIGIT