工具类apache.commons.lang.StringUtils 使用心得
2015-09-01 09:57
916 查看
转自 http://www.cnblogs.com/jifeng/archive/2012/08/05/2623767.html
在JAVA中我们用的最多的类应该就是String了。对于String的处理说简单也简单,但是有的时候要自己去实现一些功能还是要浪费一点时间的。一年之前接触了StringUtils这个工具类,就猛然爱上了它,日复一日心里始终觉得这东西实在太好了。不敢独享,所以决定要总结一下个人使用StringUtils的一些心得。
1.StringUtils.isEmpty(String str)
经常需要去判断一个字符串是否为空,null,""。使用该方法可以轻松得到结论:
2.StringUtils.isNotEmpty(String str)
判断字符串是否非空,通过查看commons.lang.StringUtils的源码可以发现isNotEmpty是这样实现的:
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
使用和1中一样的参数调用isNotEmpty(String str)得到的结果:
StringUtils.isNotEmpty(null);//false
StringUtils.isNotEmpty("");//false
StringUtils.isNotEmpty(" ");//true
StringUtils.isNotEmpty(" ");//true
StringUtils.isNotEmpty("aa");//true
StringUtils.isNotEmpty(" aaa ");//true
3.StringUtils.isBlank(String str)
判断字符串是否为空和空字符
System.out.println(StringUtils.isBlank(null));//true
System.out.println(StringUtils.isBlank(""));//true
System.out.println(StringUtils.isBlank(" "));//true
System.out.println(StringUtils.isBlank(" "));//true
System.out.println(StringUtils.isBlank("\n\t"));//true
System.out.println(StringUtils.isBlank("aaa"));//false
System.out.println(StringUtils.isBlank(" aa "));//false
4.StringUtils.isNotBlank(String str)
和之前说的isNotEmpty(String str) 一样该方法也是: retrurn !isBlank(str);
5.StringUtils.defaultIfEmpty(String str, String defalutValue)
如果字符串为""或者 null 则替换成参数2中的字符串:
System.out.println("1: " + StringUtils.defaultIfEmpty("", "a"));//1: a
System.out.println("2: " + StringUtils.defaultIfEmpty("\n\t", "a"));//2:
System.out.println("3: " + StringUtils.defaultIfEmpty("", "a"));//3: a
System.out.println("4: " + StringUtils.defaultIfEmpty(" ", "a"));//4:
System.out.println("5: " + StringUtils.defaultIfEmpty("aaa", "a"));//5: aaa
System.out.println("6: " + StringUtils.defaultIfEmpty(" aaa ", "a"));//6: aaa
System.out.println("7: " + StringUtils.defaultIfEmpty(null, "a"));//7: a
6. StringUtils.defaultString(String str, String defaultValue)
和5相似:
System.out.println("1: " + StringUtils.defaultString(null, "a"));//1: a
System.out.println("2: " + StringUtils.defaultString("", "a"));//2:
System.out.println("3: " + StringUtils.defaultString(" ", "a"));//3:
System.out.println("4: " + StringUtils.defaultString("\n\t", "a"));//4:
System.out.println("5: " + StringUtils.defaultString("aa", "a"));//5: aa
System.out.println("6: " + StringUtils.defaultString(" aaa", "a"));//6: aaa
7.StringUtils.capitalize(String str)
首字母大写
System.out.println(StringUtils.capitalize("xxxx"));//Xxxx
System.out.println(StringUtils.capitalize("Xxxx"));//Xxxx
System.out.println(StringUtils.capitalize(" xxxx"));// xxxx
8.StringUtils.remove(String str, String removeStr)
从str中去除removeStr
System.out.println(StringUtils.remove("abcd", "ab"));//cd
System.out.println(StringUtils.remove("abcd", "ad"));//abcd
类似方法有
StringUtils.removeEnd(arg0, arg1)
StringUtils.removeStart(arg0, arg1)
9.StringUtils.countMatches(String str, String find)
计算字符串在另一个字符串中出现的次数:
int nCount = StringUtils.countMatches("UPDATE tb_table SET xx=?,xyz=?, sss=? WHERE id=?", "?");
nCount = 4
这个方法甚妙!个人使用这个方法写了一个自动给预编译语句设置参数的方法,用起来得心应手省了不少事!
更多信息请查看
10.常用用法举例
http://www.cnblogs.com/jifeng/archive/2012/08/05/2623767.html
在JAVA中我们用的最多的类应该就是String了。对于String的处理说简单也简单,但是有的时候要自己去实现一些功能还是要浪费一点时间的。一年之前接触了StringUtils这个工具类,就猛然爱上了它,日复一日心里始终觉得这东西实在太好了。不敢独享,所以决定要总结一下个人使用StringUtils的一些心得。
1.StringUtils.isEmpty(String str)
经常需要去判断一个字符串是否为空,null,""。使用该方法可以轻松得到结论:
StringUtils.isEmpty(null) = true |
StringUtils.isEmpty("") = true |
StringUtils.isEmpty(" ") = false |
StringUtils.isEmpty(" ") = false |
StringUtils.isEmpty("aa") = false |
StringUtils.isEmpty(" aaa ") = false |
2.StringUtils.isNotEmpty(String str)
判断字符串是否非空,通过查看commons.lang.StringUtils的源码可以发现isNotEmpty是这样实现的:
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
使用和1中一样的参数调用isNotEmpty(String str)得到的结果:
StringUtils.isNotEmpty(null);//false
StringUtils.isNotEmpty("");//false
StringUtils.isNotEmpty(" ");//true
StringUtils.isNotEmpty(" ");//true
StringUtils.isNotEmpty("aa");//true
StringUtils.isNotEmpty(" aaa ");//true
3.StringUtils.isBlank(String str)
判断字符串是否为空和空字符
System.out.println(StringUtils.isBlank(null));//true
System.out.println(StringUtils.isBlank(""));//true
System.out.println(StringUtils.isBlank(" "));//true
System.out.println(StringUtils.isBlank(" "));//true
System.out.println(StringUtils.isBlank("\n\t"));//true
System.out.println(StringUtils.isBlank("aaa"));//false
System.out.println(StringUtils.isBlank(" aa "));//false
4.StringUtils.isNotBlank(String str)
和之前说的isNotEmpty(String str) 一样该方法也是: retrurn !isBlank(str);
5.StringUtils.defaultIfEmpty(String str, String defalutValue)
如果字符串为""或者 null 则替换成参数2中的字符串:
System.out.println("1: " + StringUtils.defaultIfEmpty("", "a"));//1: a
System.out.println("2: " + StringUtils.defaultIfEmpty("\n\t", "a"));//2:
System.out.println("3: " + StringUtils.defaultIfEmpty("", "a"));//3: a
System.out.println("4: " + StringUtils.defaultIfEmpty(" ", "a"));//4:
System.out.println("5: " + StringUtils.defaultIfEmpty("aaa", "a"));//5: aaa
System.out.println("6: " + StringUtils.defaultIfEmpty(" aaa ", "a"));//6: aaa
System.out.println("7: " + StringUtils.defaultIfEmpty(null, "a"));//7: a
6. StringUtils.defaultString(String str, String defaultValue)
和5相似:
System.out.println("1: " + StringUtils.defaultString(null, "a"));//1: a
System.out.println("2: " + StringUtils.defaultString("", "a"));//2:
System.out.println("3: " + StringUtils.defaultString(" ", "a"));//3:
System.out.println("4: " + StringUtils.defaultString("\n\t", "a"));//4:
System.out.println("5: " + StringUtils.defaultString("aa", "a"));//5: aa
System.out.println("6: " + StringUtils.defaultString(" aaa", "a"));//6: aaa
7.StringUtils.capitalize(String str)
首字母大写
System.out.println(StringUtils.capitalize("xxxx"));//Xxxx
System.out.println(StringUtils.capitalize("Xxxx"));//Xxxx
System.out.println(StringUtils.capitalize(" xxxx"));// xxxx
8.StringUtils.remove(String str, String removeStr)
从str中去除removeStr
System.out.println(StringUtils.remove("abcd", "ab"));//cd
System.out.println(StringUtils.remove("abcd", "ad"));//abcd
类似方法有
StringUtils.removeEnd(arg0, arg1)
StringUtils.removeStart(arg0, arg1)
9.StringUtils.countMatches(String str, String find)
计算字符串在另一个字符串中出现的次数:
int nCount = StringUtils.countMatches("UPDATE tb_table SET xx=?,xyz=?, sss=? WHERE id=?", "?");
nCount = 4
这个方法甚妙!个人使用这个方法写了一个自动给预编译语句设置参数的方法,用起来得心应手省了不少事!
更多信息请查看
10.常用用法举例
http://www.cnblogs.com/jifeng/archive/2012/08/05/2623767.html
相关文章推荐
- java IO总结
- 从JavaSE开始到Android开发
- 2014134027谢天
- 2014134027谢天
- 谢天2014134027课程实验报告
- 2014134027 谢天
- JavaSE:Day01-Java概述
- 黑马程序员 JAVASE——异常及异常的处理机制
- 黑马程序员 JAVASE——String
- 黑马程序员 JAVA提高——反射
- 黑马程序员 java提高——张孝详交通灯管理系统
- 黑马程序员 java提高——张孝祥银行业务调度系统
- java就是这样一个东西
- 史上最全Java学习线路之基本
- JAVA之StringBuffer与StringBuilder
- 从一个无所事事,生活极其没有规律的待业青年
- 回顾刚来的那一天还历历在目,不禁感概一番
- windows和linux下的JDK环境变量配置
- 黑马程序员——JavaSE基础_2
- 黑马程序员——JavaSE基础_3