📜  CharMatcher 类 |番石榴 |Java

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

CharMatcher 类 |番石榴 |Java

CharMatcher确定任何Java char 值的值。此类提供各种方法来处理 char 值的各种Java类型。

声明: com.google.common.base.CharMatcher的声明如下:

@GwtCompatible(emulated = true)
public final class CharMatcher
   extends Object

CharMatcher 实例有两种获取方式:

  • 使用常量: CharMatcher 类提供以下常量来获取 CharMatcher 实例。

    注意:这个类只处理 char 值。它不理解 0x10000 到 0x10FFFF 范围内的补充 Unicode 代码点。此类逻辑字符使用代理对编码为字符串,并且 CharMatcher 将它们视为两个单独的字符。

  • 使用 CharMatcher 类提供的方法:可以使用以下方法获取 CharMatcher 类的实例。

示例 1:

// Java code to get number of matching
// characters in given sequence
// and display them using countIn()
  
  
import com.google.common.base.CharMatcher;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        // "anyOf" method returns a char matcher
        // that matches any character present in
        // the given character sequence.
        CharMatcher matcher = CharMatcher.anyOf("aeiou");
  
        String str = "Hello GeeksforGeeks, What's up ?";
  
        // "countIn" returns the number of matching
        // characters found in a character sequence.
        int vowels = matcher.countIn(str);
  
        // To display the number of vowels in
        // character sequence
        System.out.println("Number of vowels in '"
                           + str + "' are "
                           + vowels);
    }
}
输出:
Number of vowels in 'Hello GeeksforGeeks, What's up ?' are 9

CharMatcher 类的其他一些方法是:

示例 2:

// Java code to get the index
// of matching character
  
import com.google.common.base.CharMatcher;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        // "anyOf" method returns a char matcher
        // that matches any character present in
        // the given character sequence.
        CharMatcher matcher = CharMatcher.anyOf("aeiou");
  
        String str = "Hello GeeksforGeeks, What's up ?";
  
        // To return the index of first matching
        // character in given input sequence.
        int firstIndex = matcher.indexIn(str);
  
        // To Return the index of first matching
        // character in given input sequence,
        // from given starting index.
        int nextIndex = matcher.indexIn(str, firstIndex + 1);
  
        // To return the index of the last matching
        // character in a character sequence
        int lastIndex = matcher.lastIndexIn(str);
  
        System.out.println("First Index is " + firstIndex);
        System.out.println("Next Index is " + nextIndex);
        System.out.println("Last Index is " + lastIndex);
    }
}
输出:
First Index is 1
Next Index is 4
Last Index is 28

CharMatcher 类的其他一些方法是:

示例 3:

// Java code to remove all digits 
// from a  given string
  
import com.google.common.base.CharMatcher;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        // Determines whether a character is
        // a digit according to Unicode.
        CharMatcher matcher = CharMatcher.DIGIT;
  
        String str = "12345Hello GeeksforGeeks1287 What's 9886up";
        System.out.println("Original String : " + str);
  
        // To remove all matching characters
        // from given string.
        String result = matcher.removeFrom(str);
  
        // To display the string which
        // doesn't contain digit
        System.out.println("After digit removal : " + result);
    }
}
输出:
Original String : 12345Hello GeeksforGeeks1287 What's 9886up
After digit removal : Hello GeeksforGeeks What's up

参考:谷歌番石榴