您的位置:首页 > 编程语言 > Java开发

guava中String的CharMatcher

2014-05-09 16:52 232 查看

1.CharMatcher的field 类型

ANY:匹配任意字符。
NONE:任何字符都不匹配
WHITESPACE:匹配字符里面的空格。
BREAKING_WHITESPACE:匹配字符之间是否被空格隔断。
INVISIBLE:确定字符是否不可见,不可见的字符包括:SPACE_SEPARATOR, LINE_SEPARATOR, PARAGRAPH_SEPARATOR, 
CONTROL, FORMAT, SURROGATE, 和PRIVATE_USE
DIGIT: Determines whether a character is a digit according to Unicode. If you only care to match ASCII digits, 
you can use inRange('0', '9').
JAVA_LETTER:Determines whether a character is a letter according to Java's definition. If you only care to 
match letters of the Latin alphabet, you can use inRange('a', 'z').or(inRange('A', 'Z'))。
JAVA_DIGIT:Determines whether a character is a digit according to Java's definition. If you only care to match
 ASCII digits, you can use inRange('0', '9')。
JAVA_LETTER_OR_DIGIT :Determines whether a character is a letter or digit according to Java's
definition.
JAVA_ISO_CONTROL :Determines whether a character is an ISO control character as specified 
by 
Character.isISOControl(char)
.
JAVA_LOWER_CASE:
Determines whether a character is lower case according to Java's
definition.
JAVA_UPPER_CASE :Determines whether a character is upper case according to Java's definition.
ASCII:Determines whether a character is ASCII, meaning that its code point is less than 128.
SINGLE_WIDTH:Determines whether a character is single-width (not double-width). When in doubt,
 this matcher errs on the side of returning false (that is, it tends to assume a character is double-width).

2.CharMatcher的经常使用方法与实例

package string;

import com.google.common.base.CharMatcher;

public class CharMatchersInstance {

public static void testCharMatchers() {
// removerFrom
String removeFromResult = CharMatcher.isNot('a').removeFrom("abacd");
System.out.println("removeForm:" + removeFromResult);
// retainFrom method
String retainFormResult = CharMatcher.is('a').retainFrom("abacd");
System.out.println("retainForm:" + retainFormResult);
// replaceFrom method
String replaceFormResult1 = CharMatcher.WHITESPACE.replaceFrom("a bcd",
'f');
System.out.println("replaceFrom_1:" + replaceFormResult1);
String replaceFormResult2 = CharMatcher.DIGIT.replaceFrom("a3bcd",
"Three");
System.out.println("replaceFrom_2:" + replaceFormResult2);
// trimFrom
String trimFromResult = CharMatcher.anyOf("ab").trimFrom("abacatabb");
System.out.println("trimFrom:" + trimFromResult);
// trimLeadingFrom
String trimLeadingFromResult = CharMatcher.anyOf("ab").trimLeadingFrom(
"abacatabb");
System.out.println("trimLeadingFrom:" + trimLeadingFromResult);
// trimTrailingFrom
String trimTrailingFromResult = CharMatcher.anyOf("ab")
.trimTrailingFrom("abacatabb");
System.out.println("trimTrailingFrom:" + trimTrailingFromResult);
// collapseFrom
String collapseFromResult = CharMatcher.anyOf("bre").collapseFrom(
"bookkeeper", '-');
System.out.println("collapseFrom:" + collapseFromResult);
// trimAndCollapseFrom
String trimAndCollapseFromResult = CharMatcher.anyOf("bre")
.trimAndCollapseFrom("bookkeeper", '-');
System.out.println("trimAndCollapseFrom:" + trimAndCollapseFromResult);
// matchesAllOf
boolean matchesAllOfResult = CharMatcher.JAVA_UPPER_CASE
.matchesAnyOf("hcd");
System.out.println("matchesAnyOf:" + matchesAllOfResult);
// or and negate
String orResult = CharMatcher.JAVA_DIGIT
.or(CharMatcher.JAVA_UPPER_CASE).retainFrom("dd59cF");
System.out.println("or:" + orResult);
// negate
String negateResult = CharMatcher.JAVA_DIGIT.negate().retainFrom(
"dd59cF");
System.out.println("negate:" + negateResult);
}

public static void main(String[] args) {

testCharMatchers();
}

}

运行结果:
removeForm:aa
retainForm:aa

replaceFrom_1:afbcd

replaceFrom_2:aThreebcd

trimFrom:cat

trimLeadingFrom:catabb

trimTrailingFrom:abacat

collapseFrom:-ookk-p-

trimAndCollapseFrom:ookk-p

matchesAnyOf:false

or:59F

negate:ddcF
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  guava charMatcher Java