CharMatcher提供了各种方法来处理各种JAVA char类型值。
以下是com.google.common.base.CharMatcher类的声明:
@GwtCompatible(emulated=true)
public final class CharMatcher
extends Object
字体
构造函数
类方法
这个类继承了以下类方法:java.lang.Object
使用所选择的编辑器创建下面的java程序 C:/> Guava
GuavaTester.java
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testCharMatcher();
}
private void testCharMatcher(){
System.out.println(CharMatcher.DIGIT.retainFrom("mahesh123")); // only the digits
System.out.println(CharMatcher.WHITESPACE.trimAndCollapseFrom(" Mahesh Parashar ", ' '));
// trim whitespace at ends, and replace/collapse whitespace into single spaces
System.out.println(CharMatcher.JAVA_DIGIT.replaceFrom("mahesh123", "*")); // star out all digits
System.out.println(CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_LOWER_CASE).retainFrom("mahesh123"));
// eliminate all characters that aren't digits or lowercase
}
}
验证结果
使用javac编译器编译如下类
C:\Guava>javac GuavaTester.java
现在运行GuavaTester看到的结果
C:\Guava>java GuavaTester
看看结果
123
Mahesh Parashar
mahesh***
mahesh123