当前位置:   article > 正文

java commons-lang3 包中 StringUtils 处理字符串的方法介绍_stringutils.repeat

stringutils.repeat

java 内置了很多处理字符串的方法,这些方法在处理空字符串的时候 如果没有人为判断是否为null 或者空时,可能会报NPE 异常,在网上看了很多处理方法,找到了 apache 出的 commons-lang3 包,专门重写了 java 内置的方法,又号称 java 的第二api。

在springboot中 加入maven组件

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-lang3</artifactId>
  4. <version>3.9</version>
  5. </dependency>

引入StringUtils

import org.apache.commons.lang3.StringUtils;

字符串比较 

equals 比较字符串是否相等,区分大小写

equalsAny 查询字符串是否在多个字符串是否存在

equalsIgnoreCase 比较字符串是否相等,不区分大小写

  1. //判断两个字符是否相等,区分大小写
  2. System.out.println(StringUtils.equals("aab", "bba")); // false
  3. //判断第一个字符串,是否在后面的字符串中匹配任何一个
  4. System.out.println(StringUtils.equalsAny("aab", "bba", "aab")); // true
  5. //判断两个字符串是否相等,不区分大小写
  6. System.out.println(StringUtils.equalsIgnoreCase("aab", "AAB")); //true

字符串不同比较

difference  比较第一个字符串和第二个字符串中不同的字符,如果第一个字符都不相同,直接返回第二个字符串,否则依次比较,直到不同的字符后全部返回

  1. //比较第一个字符串和第二个字符串中不同的字符,如果第一个字符都不相同,直接返回第二个字符串,否则依次比较,直到不同的字符后全部返回
  2. System.out.println(StringUtils.difference("women", "dwoxyzmen")); //dwoxyzmen
  3. System.out.println(StringUtils.difference("women", "woxyzmen")); //xyzmen

判断空串

isBlank

isEmpty

  1. //判断是否为空(注:isBlank与isEmpty 区别)
  2. StringUtils.isBlank(null); // true
  3. StringUtils.isBlank(""); // true
  4. StringUtils.isBlank(" ");//true
  5. StringUtils.isNoneBlank(" ", "dsy");//false
  6. StringUtils.isEmpty(null); //true
  7. StringUtils.isEmpty("");//true
  8. StringUtils.isEmpty(" ");//false
  9. System.out.println(StringUtils.isNoneEmpty(" ", "bbs"));//true

字符串连接、替换

join 连接

replace 替换

replaceEach 依次替换

repeat 字符串重复

reverse 字符串翻转

trim 字符串去空格

chomp 字符串去 换行符

  1. //连接list 为字符串
  2. List<Integer> list = new ArrayList<>();
  3. list.add(1);
  4. list.add(2);
  5. list.add(4);
  6. list.add(5);
  7. String s = StringUtils.join(list, ":");
  8. System.out.println(s); // 1:2:4:5
  9. //字符串替换
  10. System.out.println(StringUtils.replace("china good", "g", "b")); //china bood
  11. //字符串批量替换
  12. System.out.println(StringUtils.replaceEach("china good", new String[] {"ch", "go"}, new String[] {"gd", "su"})); //gdina suod
  13. //字符串重复10次
  14. System.out.println(StringUtils.repeat("ab ", 10)); //ab ab ab ab ab ab ab ab ab ab
  15. //字符串反转
  16. System.out.println(StringUtils.reverse("I am 30th years")); // sraey ht03 ma I
  17. //去除字符串首位空格
  18. System.out.println(StringUtils.trim(" ad b s ")); //ad b s
  19. //去掉字符串中的 \r 、 \n 或 \r\n
  20. System.out.println(StringUtils.chomp("abcd\r\n")); // abcd

数字字符串

isNumberic 判断是否全数字字符串

isNumbericSpace 判断是否全数字字符串,排除空格

  1. //判断是否全数字字符串
  2. System.out.println(StringUtils.isNumeric("889")); // true
  3. System.out.println(StringUtils.isNumeric("885 ")); // false
  4. //判断是否都是数字,排除空格
  5. System.out.println(StringUtils.isNumericSpace("72 3 ")); // true

字符串大小写转换和判断 

capitalize  首字母转大写

upcapitalize  首字符转小写

isAllUpperCase 是否所有字符都是大写

isAllLowerCase 是否所有字符都是小写

upperCase 所有字符转大写

lowerCase 所有字符转小写

sawpCase 把字符串大小写交换

  1. import org.apache.commons.lang3.StringUtils;
  2. //字符串第一个字符转大写
  3. System.out.println(StringUtils.capitalize("good")); //Good
  4. //字符串第一个字符转小写
  5. System.out.println(StringUtils.uncapitalize("Sdood")); //sdood
  6. //判断字符串是否全大写或者全小写
  7. System.out.println(StringUtils.isAllUpperCase("Aab")); // false
  8. System.out.println(StringUtils.isAllLowerCase("Aab")); // false
  9. //小写转大写
  10. System.out.println(StringUtils.upperCase("bbs")); //BBS
  11. //大写转小写
  12. System.out.println(StringUtils.lowerCase("AAB")); //aab
  13. //字符串大小写反转
  14. System.out.println(StringUtils.swapCase("I am a Boy")); //i AM A bOY

字符串中查找子串 

contains 查询子串在母串中是否存在,区分大小写

containsIngoreCase 查询子串在母串中是否存在,不区分大小写

countMatches  查询子串在母串中出现的次数

deleteWhiteSpace 去掉字符串中所有的空格

  1. //判断字符串child 中是否包含 3il
  2. System.out.println(StringUtils.contains("child", "3il")); // false
  3. //判断字符串child中是否包含 Il,不区分大小写
  4. System.out.println(StringUtils.containsIgnoreCase("child", "Il")); // true
  5. //查询字符t 在字符串冲出现的次数
  6. System.out.println(StringUtils.countMatches("today Tis tuesday", "t")); //2
  7. //去掉字符串中的所有空格
  8. System.out.println(StringUtils.deleteWhitespace("today is tuesday")); //todayistuesday

字符串搜索是否存在

startsWith  检查是否匹配字符串开始

startsWithIgnoreCase  检查是否匹配字符串开始,忽略大小写

startsWithAny  检查是否匹配字符串开始,从多个字符串中任意一个匹配

endsWith

endsWithIgnoreCase

endsWithAny

  1. //检查是否匹配字符串开始
  2. System.out.println(StringUtils.startsWith("good afternoon", "good a")); // true
  3. //忽略大小写
  4. System.out.println(StringUtils.startsWithIgnoreCase("good afternoon", "GooD a")); // true
  5. //在多个字符串中任意一个存在
  6. System.out.println(StringUtils.startsWithAny("good afternoon", "good a")); // true
  7. //检查是否匹配字符串结尾
  8. System.out.println(StringUtils.endsWith("womens", "ens")); //true
  9. //检查是否匹配字符串结尾,不区分大小写
  10. System.out.println(StringUtils.endsWithIgnoreCase("womens", "Ens")); //true
  11. //检查是否匹配字符串结尾,在后面多个字符串中任意一个存在
  12. System.out.println(StringUtils.endsWithAny("womens", new String[] {"xyz", "Ens"})); //false

indexOf 

lastIndexOf

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

字符串填充

abbreviate  使用... 填充到指定长度

center 两边填充空格到指定长度

appendIfMissing 

  1. //缩短字符串到某长度,用...结尾.其实就是 abbreviate 第二个参数maxWidth , (substring(str, 0, maxWidth-3) + "...")
  2. System.out.println(StringUtils.abbreviate("abcdefghji", 7)); // abcd...
  3. //字符串扩充到7个字符,abdc在中间,空格分布在两边,如下,左边一个空格,右边两个空格
  4. System.out.println(StringUtils.center("abcd", 7)); // " abcd "
  5. //字符串结尾的后缀是否与你要结尾的后缀匹配,若不匹配则添加后缀
  6. System.out.println(StringUtils.appendIfMissing("abcxyz", "axyz")); //abcxyzaxyz

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/1005567
推荐阅读
相关标签
  

闽ICP备14008679号