赞
踩
java 内置了很多处理字符串的方法,这些方法在处理空字符串的时候 如果没有人为判断是否为null 或者空时,可能会报NPE 异常,在网上看了很多处理方法,找到了 apache 出的 commons-lang3 包,专门重写了 java 内置的方法,又号称 java 的第二api。
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.9</version>
- </dependency>
引入StringUtils
import org.apache.commons.lang3.StringUtils;
-
- //判断两个字符是否相等,区分大小写
- System.out.println(StringUtils.equals("aab", "bba")); // false
-
- //判断第一个字符串,是否在后面的字符串中匹配任何一个
- System.out.println(StringUtils.equalsAny("aab", "bba", "aab")); // true
-
- //判断两个字符串是否相等,不区分大小写
- System.out.println(StringUtils.equalsIgnoreCase("aab", "AAB")); //true
- //比较第一个字符串和第二个字符串中不同的字符,如果第一个字符都不相同,直接返回第二个字符串,否则依次比较,直到不同的字符后全部返回
- System.out.println(StringUtils.difference("women", "dwoxyzmen")); //dwoxyzmen
- System.out.println(StringUtils.difference("women", "woxyzmen")); //xyzmen
- //判断是否为空(注:isBlank与isEmpty 区别)
- StringUtils.isBlank(null); // true
- StringUtils.isBlank(""); // true
- StringUtils.isBlank(" ");//true
- StringUtils.isNoneBlank(" ", "dsy");//false
-
- StringUtils.isEmpty(null); //true
- StringUtils.isEmpty("");//true
- StringUtils.isEmpty(" ");//false
- System.out.println(StringUtils.isNoneEmpty(" ", "bbs"));//true
-
- //连接list 为字符串
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(4);
- list.add(5);
-
- String s = StringUtils.join(list, ":");
- System.out.println(s); // 1:2:4:5
-
-
- //字符串替换
- System.out.println(StringUtils.replace("china good", "g", "b")); //china bood
- //字符串批量替换
- System.out.println(StringUtils.replaceEach("china good", new String[] {"ch", "go"}, new String[] {"gd", "su"})); //gdina suod
-
- //字符串重复10次
- System.out.println(StringUtils.repeat("ab ", 10)); //ab ab ab ab ab ab ab ab ab ab
-
- //字符串反转
- System.out.println(StringUtils.reverse("I am 30th years")); // sraey ht03 ma I
-
- //去除字符串首位空格
- System.out.println(StringUtils.trim(" ad b s ")); //ad b s
-
- //去掉字符串中的 \r 、 \n 或 \r\n
- System.out.println(StringUtils.chomp("abcd\r\n")); // abcd

- //判断是否全数字字符串
- System.out.println(StringUtils.isNumeric("889")); // true
- System.out.println(StringUtils.isNumeric("885 ")); // false
-
- //判断是否都是数字,排除空格
- System.out.println(StringUtils.isNumericSpace("72 3 ")); // true
- import org.apache.commons.lang3.StringUtils;
-
- //字符串第一个字符转大写
- System.out.println(StringUtils.capitalize("good")); //Good
-
- //字符串第一个字符转小写
- System.out.println(StringUtils.uncapitalize("Sdood")); //sdood
-
- //判断字符串是否全大写或者全小写
- System.out.println(StringUtils.isAllUpperCase("Aab")); // false
-
- System.out.println(StringUtils.isAllLowerCase("Aab")); // false
-
- //小写转大写
- System.out.println(StringUtils.upperCase("bbs")); //BBS
-
- //大写转小写
- System.out.println(StringUtils.lowerCase("AAB")); //aab
-
- //字符串大小写反转
- System.out.println(StringUtils.swapCase("I am a Boy")); //i AM A bOY

- //判断字符串child 中是否包含 3il
- System.out.println(StringUtils.contains("child", "3il")); // false
-
- //判断字符串child中是否包含 Il,不区分大小写
- System.out.println(StringUtils.containsIgnoreCase("child", "Il")); // true
-
- //查询字符t 在字符串冲出现的次数
- System.out.println(StringUtils.countMatches("today Tis tuesday", "t")); //2
-
- //去掉字符串中的所有空格
- System.out.println(StringUtils.deleteWhitespace("today is tuesday")); //todayistuesday
- //检查是否匹配字符串开始
- System.out.println(StringUtils.startsWith("good afternoon", "good a")); // true
- //忽略大小写
- System.out.println(StringUtils.startsWithIgnoreCase("good afternoon", "GooD a")); // true
-
- //在多个字符串中任意一个存在
- System.out.println(StringUtils.startsWithAny("good afternoon", "good a")); // true
-
-
- //检查是否匹配字符串结尾
- System.out.println(StringUtils.endsWith("womens", "ens")); //true
-
- //检查是否匹配字符串结尾,不区分大小写
- System.out.println(StringUtils.endsWithIgnoreCase("womens", "Ens")); //true
-
- //检查是否匹配字符串结尾,在后面多个字符串中任意一个存在
- System.out.println(StringUtils.endsWithAny("womens", new String[] {"xyz", "Ens"})); //false
-

-
- //查找字符串i第一次出现的位置
- System.out.println(StringUtils.indexOf("china is big", "i")); // 2
-
- //查找字符串i第一次出现的位置,从第5个字符开始查找
- System.out.println(StringUtils.indexOf("china is big", "i",5)); // 6
-
- //查询字符第n次出现的位置
- System.out.println(StringUtils.ordinalIndexOf("aabaabaa", "a", 3)); //3
-
- //反向查找
- System.out.println(StringUtils.lastIndexOf("china is big", "i")); // 10
-
- //反向查找字符i 第3次出现的位置
- System.out.println(StringUtils.lastIndexOf("china is big", "i", 3)); // 2
-
- //反向查找字符is 第2次出现的位置
- System.out.println(StringUtils.lastOrdinalIndexOf("gchisna is bisg", "is", 2)); // 8
-

abbreviate 使用... 填充到指定长度
center 两边填充空格到指定长度
appendIfMissing
- //缩短字符串到某长度,用...结尾.其实就是 abbreviate 第二个参数maxWidth , (substring(str, 0, maxWidth-3) + "...")
- System.out.println(StringUtils.abbreviate("abcdefghji", 7)); // abcd...
-
- //字符串扩充到7个字符,abdc在中间,空格分布在两边,如下,左边一个空格,右边两个空格
- System.out.println(StringUtils.center("abcd", 7)); // " abcd "
-
-
- //字符串结尾的后缀是否与你要结尾的后缀匹配,若不匹配则添加后缀
- System.out.println(StringUtils.appendIfMissing("abcxyz", "axyz")); //abcxyzaxyz
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。