Java工具类(字符串)

无视类型/是list还是Array直接进行字符拼接

lang3

StringUtils.join(List.of(1, 2, 3), ","
// 1,2,3

切分

按照某个字符串进行切分

String str2 = "abc,def";
String[] split = str.split(",");

通过正则表达式切分

String str2 = "abc,def,gch";
String[] split1 = str2.split(",|a");

限制切分数量的切分

String str2 = "abc,de,f";
String[] split = str.split(",", 2);

合并空格并强制切分

" a b  c ".strip().split("\\s+")

截取

通过开始结尾两个分隔符获取有效信息

String message = StringUtils.substringBetween("name: lix, age: 12,", "name: ", ",");
String message2 = StringUtils.substringBetween("name1: lix, age: 12,", "name: ", ",");

获取多个切分点

 String[] strings = StringUtils.substringsBetween("abcd abcd", "a", "d");

 
 

替换前N个字符

StringUtils.replace("1111122222", "11", "ab", 2)
// abab122222

在这里插入图片描述

拼接

String join = StringUtils.join(List.of(1, 2, 3), ",");
//1,2,3
String join = StringUtils.join(List.of(1, 2, 3), "");
//123

流式合并

List<Integer> list = List.of(1, 2, 3);
 String collect = list.stream().map(Object::toString).collect(Collectors.joining(","));
 //1,2,3

 
 

比较

忽略大小写比较字符串

boolean abc2 = "abc".equalsIgnoreCase("Abc");
boolean b = StringUtils.equalsIgnoreCase("abc", "ABC");

 
 

去掉多余字符

切掉前后的【】等字符

StringUtils.strip("[abc, def, gch]", "[]");
// abc, def, gch

切除空格

String strip = "  abc  ".strip();
StringUtils.trim(" abc ");
// abc

合并所有空格并只将空格只保留一个字符

String s = "a b c".replaceAll("\\s+", " ");

逗号分隔

String[] split = "a, b, c ".strip().split("\\s*,\\s*");

删除最后一个字符(假如匹配)

String s1 = StringUtils.removeEnd("abcdavbabc", "abc");
String s1 = StringUtils.removeEnd("abcdavbabc", "ab"); // 啥都不做

 
 

空白和NULL判断

StringUtils.defaultIfBlank(null, "null1");
StringUtils.defaultIfBlank("", "null2");
StringUtils.defaultIfBlank(" ", "null3");
// null1
// null2
// null3

字符串列表中null和空白替换为特定字符

List<String> stringList = new ArrayList<>(List.of("a", "b", "c", ""));
stringList.add(null);
stringList.replaceAll(x -> StringUtils.defaultIfBlank(x, ""));

比较两个字符串是否相等

boolean abc2 = Objects.equals("abc", null);

判断是否为null|空|blank lang3

String s = StringUtils.isBlank(null);
// true

删除空白字符

List<String> stringList = new ArrayList<>(List.of("a", "b", "c", ""));
stringList.removeIf(StringUtils::isBlank);
// a, b, c

 
 

全角转半角

public static String toHalfCharacter(String input) {
        if (input == null) {
            return null;
        }
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);

 
 

其他操作

字符串翻转

String s1 = new StringBuilder("abc").reverse().toString();
// cba
String abc = StringUtils.reverse("abc");
//cba

通过boolen进行映射

String s = BooleanUtils.toString(true, "true", "false");

展示所有类型字符串

需要org.junit.platform.commons.util.StringUtils.nullSafeToString

打印一切可以打印的字符、列表、空指针、类

        System.out.println(org.junit.platform.commons.util.StringUtils.nullSafeToString(new int[]{1, 2, 4}));
        System.out.println(org.junit.platform.commons.util.StringUtils.nullSafeToString(null));
        System.out.println(org.junit.platform.commons.util.StringUtils.nullSafeToString(Collections.EMPTY_LIST));
	// null
	// []
	// [1, 2, 3, 4]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值