简化你的 java 字符串操作:Guava 之 CharMatcher 用法简介

摘要:简化你的 java 字符串操作:Guava 之 CharMatcher 用法简介

ref:http://my.oschina.net/leejun2005/blog/178569

String str = "FirstName LastName +1 123 456 789 !@#$%^&*()_+|}{:\"?><";
// Use a predefined constant (predefine CharMatcher)
CharMatcher.DIGIT.retainFrom(str);
 
Output:->
"1123456789"
 
CharMatcher.JAVA_LETTER.retainFrom(str);
 
Output:->
"FirstNameLastName"
 
CharMatcher.JAVA_LETTER_OR_DIGIT.retainFrom(str);
 
Output:->
"FirstNameLastName1123456789"
 
CharMatcher.ANY.countIn(str)
 
Output:->
54
 
CharMatcher.DIGIT.countIn(str);
 
Output:->
10