总结StringUtils工具类字符串操作的方法

StringUtils工具类

Apache commons lang包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便。在这里将常用的方法总结了一下.要想使用类必须要先导入commons-lang-X.jar文件到项目中

   StringUtils类在操作字符串时,即使操作的为null值也是安全的,不会报NullPointerException,这一点在后面的例子中再具体说明。因此,在操作字符串时使用StringUtils相比使用原生的String会更加安全。

非空判断

StringUtils中非空判断是最常用的,其方法主要有以下几个

  1. boolean StringUtils.isBlank(String str);
  2. boolean StringUtils.isNotBlank(String str);
  3. boolean StringUtils.isEmpty(String str);
  4. boolean StringUtils.isNotEmpty(String str);
  5. boolean StringUtils.isWhitespace(arg0);

isBlankisNotBlank,isEmpty与isNotEmpty是是非关系.通过下面的实例具体来看下三种方法判断非空的区别:

System.out.println(StringUtils.isBlank("")); // true

System.out.println(StringUtils.isBlank(" ")); // true

System.out.println(StringUtils.isBlank("     ")); // true

System.out.println(StringUtils.isBlank("\t")); // true

System.out.println(StringUtils.isBlank("\r")); // true

System.out.println(StringUtils.isBlank("\n")); // true

System.out.println(StringUtils.isBlank(null)); // true

 

System.out.println(StringUtils.isEmpty("")); // true

System.out.println(StringUtils.isEmpty(" ")); // false

System.out.println(StringUtils.isEmpty("     ")); // false

System.out.println(StringUtils.isEmpty("\t")); // false

System.out.println(StringUtils.isEmpty("\r")); // false

System.out.println(StringUtils.isEmpty("\n")); // false

System.out.println(StringUtils.isEmpty(nu ll)); // true

 

System.out.println(StringUtils.isWhitespace("")); // true

System.out.println(StringUtils.isWhitespace(" ")); // true

System.out.println(StringUtils.isWhitespace("    ")); // true

System.out.println(StringUtils.isWhitespace("\t")); // true

System.out.println(StringUtils.isWhitespace("\r")); // true

System.out.println(StringUtils.isWhitespace("\n")); // true

System.out.println(StringUtils.isWhitespace(null)); // false

上面的结果可以看到:

blank:代表的是空串(""),空白符(空格""," ",制表符"\t",回车符"\r","\n"等)及null值;
  empty:代表的是空串("")和null值,不包含空白符;
   whitespace:包含空串("")和空白符,不包含null值.

转换

  1. 字符串首字母大小写转换

// null (注意此处不会报异常)

System.out.println(StringUtils.capitalize(null));  //null

//(首字母转大写)

System.out.println(StringUtils.capitalize("china")); // China System.out.println(StringUtils.uncapitalize(null)); // null  

//(首字母转小写)

System.out.println(StringUtils.uncapitalize("CHINA")); // cHINA  

  1. 字符串整体大小写转换

//(全部转为大写)

System.out.println(StringUtils.upperCase(null)); // null

System.out.println(StringUtils.upperCase("china")); // CHINA

// CHINA (按照指定规则转换为大写)

System.out.println(StringUtils.upperCase("china", Locale.ENGLISH));

//(全部转换为小写)

System.out.println(StringUtils.lowerCase(null)); // null

System.out.println(StringUtils.lowerCase("CHINA")); // china

// china (按照指定转换规则转换为小写)

System.out.println(StringUtils.lowerCase("CHINA", Locale.ENGLISH));

  1. 字符串大小写互换

System.out.println(StringUtils.swapCase(null)); // null

System.out.println(StringUtils.swapCase("chINA")); // CHina

  1. 判断字符串是否全部是大写或小写(空或空白符均为false)

System.out.println(StringUtils.isAllUpperCase(null)); // false

System.out.println(StringUtils.isAllUpperCase("")); // false

System.out.println(StringUtils.isAllUpperCase(" ")); // false

System.out.println(StringUtils.isAllUpperCase("CHINA")); // true

System.out.println(StringUtils.isAllLowerCase(null)); // false

System.out.println( StringUtils.isAllLowerCase("")); // false

System.out.println( StringUtils.isAllLowerCase(" ")); // false

System.out.println(StringUtils.isAllLowerCase("china")); // true

移除

从字符串中移除匹配的字符或字符序列,如果要移除的字符或字符序列在字符串中不存在,即无匹配,则不进行移除

  1. 移除单个字符

System.out.println(StringUtils.remove(null, 'a')); // null  

System.out.println(StringUtils.remove("china", null)); // china

System.out.println(StringUtils.remove("china", 'i')); // chna

//(如果要移除的字符不存在,则返回原字符串)

System.out.println(StringUtils.remove("china", 'b')); // china

  1. 移除指定字符串

System.out.println(StringUtils.remove("china", "in")); // cha

System.out.println(StringUtils.remove("china", "nin")); // china

  1. 移除开头匹配字符串

System.out.println(StringUtils.removeStart("china", "ch")); // ina

// na (忽略大小写)

System.out.println(StringUtils.removeStartIgnoreCase("china","CHI"));

  1. 移除结尾匹配的字符串

System.out.println(StringUtils.removeEnd("china", "na")); // chi

// chi (忽略大小写)

System.out.println(StringUtils.removeEndIgnoreCase("china", "NA"));

  1. 移除空白字符

System.out.println(StringUtils.deleteWhitespace(null)); //null

System.out.println(StringUtils.deleteWhitespace(" c h  i\tn\ra")); // china

替换

 StringUtils中常用的替换方法有如下几种

  1. 替换单个字符或字符序列
    1. replace方法用于替换单个字符序列
      1. 单个字符序列

StringUtils.replace("china", null, "z"));// china

//(此处被替换字符序列为null,因此替换会被忽略,返回原字符串)

         StringUtils.replace("china", "c", null)); // china

//(此处替换字符序列为null,因此替换也被忽略,返回原字符串)

        StringUtils.replace("china", "a", "ese")); // chinese

         StringUtils.replace("china", "a", "")); // chin

      1. 指定最大替换的个数

StringUtils.replace("aabaaa", "a", "z", 0)); // aabaaa 

//(0表示替换的个数为0,也就是不替换)

         StringUtils.replace("aabaaa", "a", "z", 1)); // zabaaa 

//(1表示最多替换1个)

         StringUtils.replace("aabaaa", "a", "z", 2)); // zzbaaa 

//(2表示最多替换2个)

         StringUtils.replace("aabaaa", "a", "z", 3)); // zzbzaa 

//(3表示最多替换3个)

         StringUtils.replace("aabaaa", "a", "z", -1)); // zzbzaa 

//(-1表示全部替换)

 

    1. replaceChars方法替换单个字符或者单个字符序列

StringUtils.replaceChars("china", 'a', 'z')); // chinz

  StringUtils.replaceChars("china", "a", "z")); // chinz

    1. replaceOnce方法

此方法只会替换一次,也就是只会替换第一个要替换的字符序列,后面即使有匹配的字符序列也不会被替换

StringUtils.replaceOnce("abaa", "a", "z")); // zbaa

    1. overlay方法

overlay(String str,String overlay,int start,int end)方法可以在指定位置进行字符序列替换,从start索引处开始(包含)到end-1索引处为止进行替换

StringUtils.overlay("abcdef", "zzzz", 2, 4)); // abzzzzef

特殊情况:

      1. 起始索引start小于结束索引end,这时会将end作为起始索引,start作为结束索引

StringUtils.overlay("abcdef", "zzzz", 4, 2)); // abzzzzef

          StringUtils.overlay("abcdef", "zzzz", 4, 3)); // abczzzzef

          StringUtils.overlay("abcdef", "zzzz", 4, 4)); // abcdzzzzef

          StringUtils.overlay("abcdef", "zzzz", 4, 5)); // abcdzzzzf

      1. 起始索引start为负数,这时start会被当作0处理

StringUtils.overlay("abcdef", "zzzz", -1, 2)); // abcdzz

          StringUtils.overlay("abcdef", "zzzz", -2, -3)); // zzzzabcdef

      1. 结束索引end大于原始字符串的长度,这时end会被当作原始字符串长度处理

StringUtils.overlay("abcdef", "zzzz", 8, 10)); // abcdefzzzz

  1. 同时替换多个字符序列
    1. replaceEach方法

replaceEach(String text, String[] searchList, String[] replacementList)方法可以同时替换多个字符序列,但被替换和替换的字符序列的个数应该对应,否则会报异常 

// xhinz (将ch和a分别替换为x和z)

StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z" }));

// china (存在null,不进行替换)

StringUtils.replaceEach("china", null, new String[] {"x", "z" }));

// IllegalArgumentException (被替换和替换的个数不对应)

StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z", "y" }));

 

    1. replaceEachRepeatedly方法

replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)方法可以循环进行替换,具体见下面的例子:

// zhina (c被替换为x,x又被替换为z)

 StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "z" }));

但如果替换是一个死循环,则会报IllegalStateException:

StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "c" }));

// IllegalStateException (c被替换为x,x又被替换为c)

反转

  1. reverse(String str)方法

StringUtils.reverse("china")); // anihc

  1. 根据指定分隔符进行反转,分隔符之间的字符不进行反转

  StringUtils.reverseDelimited("china", ',')); // china

  StringUtils.reverseDelimited("cxhinxa", 'x')); // axhinxz

  StringUtils.reverseDelimited("c.hin.a", '.')); // a.hin.c

  StringUtils.reverseDelimited("c.hina", '.')); // hina.c

 

 

 

 

 

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android中提供了丰富的字符串操作方法,但是如果想要更加方便地操作字符串,我们可以自定义一个字符串工具类。下面是一个简单的字符串工具类示例: ```java public class StringUtils { /** * 判断字符串是否为空 * * @param str 字符串 * @return true为空,false为非空 */ public static boolean isEmpty(String str) { return str == null || str.length() == 0; } /** * 判断字符串是否为非空 * * @param str 字符串 * @return true为非空,false为空 */ public static boolean isNotEmpty(String str) { return !isEmpty(str); } /** * 判断字符串是否为空白 * * @param str 字符串 * @return true为空白,false为非空白 */ public static boolean isBlank(String str) { return str == null || str.trim().length() == 0; } /** * 判断字符串是否为非空白 * * @param str 字符串 * @return true为非空白,false为空白 */ public static boolean isNotBlank(String str) { return !isBlank(str); } /** * 截取字符串 * * @param str 字符串 * @param start 起始位置(包括) * @param end 结束位置(不包括) * @return 截取后的字符串 */ public static String substring(String str, int start, int end) { if (str == null) { return null; } if (end < 0) { end = str.length() + end; } if (start < 0) { start = str.length() + start; } if (end > str.length()) { end = str.length(); } if (start > end) { return ""; } if (start < 0) { start = 0; } if (end < 0) { end = 0; } return str.substring(start, end); } /** * 字符串替换 * * @param str 字符串 * @param oldString 要替换的字符串 * @param newString 替换成的字符串 * @return 替换后的字符串 */ public static String replace(String str, String oldString, String newString) { if (str == null) { return null; } int index = str.indexOf(oldString); if (index < 0) { return str; } StringBuilder sb = new StringBuilder(); int lastIndex = 0; while (index >= 0) { sb.append(str.substring(lastIndex, index)).append(newString); lastIndex = index + oldString.length(); index = str.indexOf(oldString, lastIndex); } if (lastIndex < str.length()) { sb.append(str.substring(lastIndex)); } return sb.toString(); } } ``` 这个工具类提供了一些常用的字符串操作方法,例如判断字符串是否为空、判断字符串是否为空白、截取字符串字符串替换等。使用这个工具类可以更加方便地操作字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@富士山下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值