/**
- on :map item之间分隔符
withKeyValueSeparator:定义map项的分隔符
*/
//转为指定格式字符串
System.out.println(Joiner.on(“;”).withKeyValueSeparator(“=”).join(ImmutableMap.of(“id”, “1”, “name”, “zs”)));//id=1;name=zs
//指定格式字符串转为map
System.out.println(Splitter.on(“;”).withKeyValueSeparator(“=”).split(“id=1;name=zs”));//{id=1, name=zs}
}
2、连接器 Joiner 小测试
小练习:
需求:
①有一个[1, , 2, null, 3]
②按逗号分隔,并去掉null和""
private static List makeList() {
List list = new ArrayList();
list.add(“1”);
list.add(“”);
list.add(“2”);
list.add(null);
list.add(“3”);
return list;//[1, , 2, null, 3]
}
static String delimiter = “,”;
//循环方式
private static void test01() {
List list = makeList();
StringBuilder builder = new StringBuilder();
for(String str : list) {
if(StringUtils.isNotEmpty(str)) {
builder.append(str+delimiter);
}
}
System.out.println(builder);//1,2,3,
builder.setLength(builder.length() - delimiter.length());
System.out.println(builder);//1,2,3
}
//Java8 stream方式
private static void test02() {
List list = makeList();
System.out.println(list.stream().filter(x -> StringUtils.isNotEmpty(x)).collect(Collectors.joining(“,”)));//1,2,3
System.out.println(list.stream().filter(StringUtils::isNotEmpty).collect(Collectors.joining(“,”)));//1,2,3
}
//Guava Joiner
private static void test03() {
List list = makeList();
System.out.println(Joiner.on(“,”).join(list));//java.lang.NullPointerException
System.out.println(Joiner.on(“,”).join(list.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList())));//1,2,3
}
// java.lang
private static void test04() {
List list = makeList();
System.out.println(String.join(“,”, list.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList())));//1,2,3
}
四、CharMatcher
常用方法如下:
-
javaDigit:数字
-
javaLowerCase:小写
-
javaUpperCase:大写
-
whitespace:空格
-
matches:匹配,返回Boolean
-
removeFrom:移除指定类型
-
retainFrom:除指定字符外全部移除
private static void test06() {
//判断是否是数字类型
System.out.println(CharMatcher.javaDigit().matches(‘1’));//true
System.out.println(CharMatcher.javaDigit().matches(‘z’));//false
//将某字符串的数字部分替换为某字符
System.out.println(CharMatcher.javaDigit().replaceFrom(“hello world666”, “1”));//hello world111
//javaLowerCase:小写
System.out.println(CharMatcher.javaLowerCase().matches(‘a’));//true
System.out.println(CharMatcher.javaLowerCase().matches(‘A’));//false
//javaUpperCase:大写
System.out.println(CharMatcher.javaUpperCase().matches(‘a’));//false
System.out.println(CharMatcher.javaUpperCase().matches(‘A’));//true
//移除空格
System.out.println(CharMatcher.whitespace().removeFrom(“hello world666”));//helloworld666
//移除数字
System.out.println(CharMatcher.javaDigit().removeFrom(“hello world666”));//hello world
//移除小写字母
System.out.println(CharMatcher.javaLowerCase().removeFrom(“hello worldAAA666”));// AAA666
//移除小写字母或数字
System.out.println(CharMatcher.javaLowerCase().or(CharMatcher.javaDigit()).removeFrom(“hello worldAAA666”));// AAA
//除指定字符外全部移除
System.out.println(CharMatcher.javaUpperCase().retainFrom(“hello worldAAA666”));//AAA
//判断某字符串包含几个某字符
System.out.println(CharMatcher.is(‘o’).countIn(“hello world”));//2
//去掉连续空格,并用指定字符替换空格
System.out.println(CharMatcher.whitespace().trimAndCollapseFrom(“hello world AAA666”, ’ '));//hello world AAA666
System.out.println(CharMatcher.whitespace().trimAndCollapseFrom(“hello world AAA666”, ''));//helloworld*AAA666
}
五、指定字符串格式转换
private static void test07() {
//hyphen 连字符;camel 骆驼
System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, “user-name”)); //userName
//underscore 下划线
System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, “user_name”)); //userName
System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, “user_name”)); //UserName
System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, “USER_name_zz”)); //UserNameZz
/**
- 感觉前面的LOWER_UNDERSCORE或者UPPER_UNDERSCORE,,都是一样的,,暂时不懂为什么
*/
System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, “USER_name_ZZ-DD”)); //UserNameZz-dd
System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, “USER_name_ZZ-DD”)); //UserNameZz-dd
}
往期精彩内容:
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!
加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
[外链图片转存中…(img-WY57flaE-1725738049774)]
[外链图片转存中…(img-bgP13OoS-1725738049775)]
[外链图片转存中…(img-6dh06aT0-1725738049776)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!
加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0