一、Joiner
拼接字符串
1、join 拼接集合中的元素
System.out.println(Joiner.on(";").join(Ints.asList(1,2,3))); //1;2;3
2、skipNullls 过滤null
Joiner joiner=Joiner.on(";").skipNulls(); System.out.println(joiner.join("1",null,"22")); //1;22
3、useForNull 替换null
Joiner joiner=Joiner.on(";").useForNull("default"); System.out.println(joiner.join("1",null,"22"));//1;default;22
4、withKeyValueSeparator 拼接map中的元素
System.out.println(Joiner.on("#").withKeyValueSeparator("=").join(ImmutableMap.of("a",1,"b",2)));//a=1#b=2
二、Splitter
分割字符串
1、split 分割字符串,转集合
Iterable<String> iterable=Splitter.on(";").split("aa;b;cc");
2、splitToList 分割字符串,转list
List<String> list=Splitter.on(";").splitToList("aa;b;cc");
3、omitEmptyStrings 过滤空串
List<String> list=Splitter.on(";").omitEmptyStrings().splitToList("aa; ;b;cc"); System.out.println(list);//[aa,b,cc]
4、trimResults 将制定charmatcher转空串
List<String> list=Splitter.on(";").trimResults(CharMatcher.anyOf("a")).splitToList("aa;b");//[ ,b]
5、limit 分割到指定个数便不再分割
List<String> list=Splitter.on(";").limit(2).splitToList("aa;b;c");//[aa,b;c]
6、转map
Map<String,String> map=Splitter.on(";").withKeyValueSeparator("=").split("a=1;b=2");//{a=1,b=2}
三、CharMatcher
找到匹配的字符串or处理匹配的字符串
1、CharMatcher本身提供了很多CharMatcher实现类,如下:
ANY: 匹配任何字符
ASCII: 匹配是否是ASCII字符
BREAKING_WHITESPACE: 匹配所有可换行的空白字符(不包括非换行空白字符,例如"u00a0")
DIGIT: 匹配ASCII数字
INVISIBLE: 匹配所有看不见的字符
JAVA_DIGIT: 匹配UNICODE数字, 使用 Character.isDigit() 实现
JAVA_ISO_CONTROL: 匹配ISO控制字符, 使用 Charater.isISOControl() 实现
JAVA_LETTER: 匹配字母, 使用 Charater.isLetter() 实现
JAVA_LETTER_OR_DIGET: 匹配数字或字母
JAVA_LOWER_CASE: 匹配小写
JAVA_UPPER_CASE: 匹配大写
NONE: 不匹配所有字符
SINGLE_WIDTH: 匹配单字宽字符, 如中文字就是双字宽
WHITESPACE: 匹配所有空白字符
以静态属性的方式使用。举个例子:
@Deprecated public static final CharMatcher JAVA_LOWER_CASE = javaLowerCase();
public static CharMatcher javaLowerCase() { return JavaLowerCase.INSTANCE; }
private static final class JavaLowerCase extends CharMatcher { static final JavaLowerCase INSTANCE = new JavaLowerCase(); @Override public boolean matches(char c) { return Character.isLowerCase(c); } @Override public String toString() { return "CharMatcher.javaLowerCase()"; } }
2、常用操作方法
CharMatcher is(char match): 返回匹配指定字符的Matcher
CharMatcher isNot(char match): 返回不匹配指定字符的Matcher
CharMatcher anyOf(CharSequence sequence): 返回匹配sequence中任意字符的Matcher
CharMatcher noneOf(CharSequence sequence): 返回不匹配sequence中任何一个字符的Matcher
CharMatcher inRange(char startInclusive, char endIncludesive): 返回匹配范围内任意字符的Matcher
CharMatcher forPredicate(Predicate<? super Charater> predicate): 返回使用predicate的apply()判断匹配的Matcher
CharMatcher negate(): 返回以当前Matcher判断规则相反的Matcher
CharMatcher and(CharMatcher other): 返回与other匹配条件组合做与来判断的Matcher
CharMatcher or(CharMatcher other): 返回与other匹配条件组合做或来判断的Matcher
boolean matchesAnyOf(CharSequence sequence): 只要sequence中有任意字符能匹配Matcher,返回true
boolean matchesAllOf(CharSequence sequence): sequence中所有字符都能匹配Matcher,返回true
boolean matchesNoneOf(CharSequence sequence): sequence中所有字符都不能匹配Matcher,返回true
int indexIn(CharSequence sequence): 返回sequence中匹配到的第一个字符的坐标
int indexIn(CharSequence sequence, int start): 返回从start开始,在sequence中匹配到的第一个字符的坐标
int lastIndexIn(CharSequence sequence): 返回sequence中最后一次匹配到的字符的坐标
int countIn(CharSequence sequence): 返回sequence中匹配到的字符计数
String removeFrom(CharSequence sequence): 删除sequence中匹配到到的字符并返回
String retainFrom(CharSequence sequence): 保留sequence中匹配到的字符并返回
String replaceFrom(CharSequence sequence, char replacement): 替换sequence中匹配到的字符并返回
String trimFrom(CharSequence sequence): 删除首尾匹配到的字符并返回
String trimLeadingFrom(CharSequence sequence): 删除首部匹配到的字符
String trimTrailingFrom(CharSequence sequence): 删除尾部匹配到的字符
String collapseFrom(CharSequence sequence, char replacement): 将匹配到的组(连续匹配的字符)替换成replacement
String trimAndCollapseFrom(CharSequence sequence, char replacement): 先trim在replace