📜  CharMatcher 字段与示例 |番石榴 |Java

📅  最后修改于: 2022-05-13 01:54:23.447000             🧑  作者: Mango

CharMatcher 字段与示例 |番石榴 |Java

CharMatcher 类提供以下常量来获取 CharMatcher 实例。


以下是其中一些

数字

CharMatcher.DIGIT根据 Unicode 判断一个字符是否为数字。如果您只关心匹配 ASCII 数字,则可以使用 inRange('0', '9')。
语法

public static final CharMatcher DIGIT

下面是上述字段的实现。

方案一:

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

下面是上述字段的实现。

方案一:

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

下面是上述字段的实现。

方案一:

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

任何

CharMatcher.ANY字段匹配任何字符,即它匹配所有字符。
语法

public static final CharMatcher ANY

下面是上述字段的实现。

方案一:

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

下面是上述字段的实现。

方案一:

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

下面是上述字段的实现。

方案一:

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

下面是上述字段的实现。

方案一:

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

下面是上述字段的实现。

方案一:

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