StringUtils--字符串工具类

StringUtils 源码,使用的是commons-lang3-3.1包。

下载地址 http://commons.apache.org/lang/download_lang.cgi

StringUtils.equals() 用法

比较两个字符串是否相等,如果两个均为null,则也认为相等

  StringUtils.equals("", "");   //结果是true

  StringUtils.equals(null, null);  //结果是true

  StringUtils.equals(null, "");  //结果是false

  StringUtils.equals("",null);  //结果是false

  StringUtils.equals(null,"");  //结果是false

  StringUtils.equalsIgnoreCase("ss", "Ss");  //不区分大小写--结果是true

判断字符串是否为空isEmpty()和isBlank()

String str1="";  
String str2=" ";  
String str3=null;  

System.out.println(StringUtils.isEmpty(str1)); //true  
System.out.println(StringUtils.isEmpty(str2)); //false  
System.out.println(StringUtils.isEmpty(str3)); //true  

System.out.println(StringUtils.isBlank(str1)); //true  
System.out.println(StringUtils.isBlank(str2)); //true  
System.out.println(StringUtils.isBlank(str3)); //true 

去空格trim()

如果字符串是null,那么返回null,不为null的时候正常去除左右空格

trimToEmpty(),如果是null值就转换成空字符串

System.out.println(StringUtils.trimToEmpty(" abc ")); //abc  
System.out.println(StringUtils.trimToEmpty(null)); //空字符串  

indexOf() 查找位置

System.out.println(StringUtils.indexOf(null, "abc")); //-1  
System.out.println(StringUtils.indexOf("aabcd", "abc")); //0  

StringUtils类的源代码提供的方法大多是与String类提供的方法重名的,使用StringUtils类最大的好处就是可以避免空指针的问题!

参考:http://blog.csdn.net/aiqinhai1016_/article/details/9929447
http://blog.csdn.net/caohaicheng/article/details/46469303

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值