Java CharMatcher类

Java CharMatcher类

CharMatcher 为任何Java字符值确定一个真或假的值。这个类提供了各种方法来处理各种Java类型的char值。

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

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

有2种方法可以获得CharMatcher实例。

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

CharMatcher类  Guava  Java

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

CharMatcher类  Guava  Java

  • 使用CharMatcher类提供的方法。 人们可以通过使用以下方法获得CharMatcher类的实例。
    CharMatcher类  Guava  Java

例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类的其他一些方法是。

CharMatcher类  Guava  Java

例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类的其他一些方法是。

CharMatcher类  Guava  Java

例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

参考资料: Google Guava

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南