StringUtils用法介绍

StringUtils是类是String类的工具类,它源自:org.apache.commons.lang3.StringUtils;包下,

org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,
是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException,
而是做了相应处理,例如,如果输入为null则返回也是null等,具体可以查看源代码)。

除了构造器,StringUtils中一共有130多个方法,并且都是static的,

所以我们可以这样调用StringUtils.xxx()。

期中几个比较常用的操作比较好用,其他的没介绍的跟String的用法大同小异差不多

package com.wyw;

import org.apache.commons.lang3.StringUtils;
/**
 * org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,
 * 是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException,
 * 而是做了相应处理,例如,如果输入为null则返回也是null等,具体可以查看源代码)。
 * 
 *除了构造器,StringUtils中一共有130多个方法,并且都是static的, 
 *
 *所以我们可以这样调用StringUtils.xxx()。 
 */
public class MyStringUtils {
    public static void main(String[] args) {
        //判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0 
        System.out.println(StringUtils.isEmpty(null));    //true
        System.out.println(StringUtils.isEmpty(""));    //true
        System.out.println(StringUtils.isEmpty(" "));    //false
        System.out.println(StringUtils.isEmpty("        "));    //false
        System.out.println("---------------------------------------------");
        //判断某字符串是否非空,等于!isEmpty(String str) 
        System.out.println(StringUtils.isNotEmpty(null));    //false
        System.out.println(StringUtils.isNotEmpty(""));        //false
        System.out.println(StringUtils.isNotEmpty(" "));    //true
        System.out.println("---------------------------------------------");
        //判断某字符串是否为空或长度为0或由空白符(whitespace)构成
        System.out.println(StringUtils.isBlank(null));  //true
        System.out.println(StringUtils.isBlank(""));    //true
        System.out.println(StringUtils.isBlank(" "));    //true
        System.out.println(StringUtils.isBlank(" r "));    //false
        System.out.println("---------------------------------------------");
        //判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成, 
        System.out.println(StringUtils.isNotBlank(null)) ;    //false
        System.out.println(StringUtils.isNotBlank(""));        //false
        System.out.println(StringUtils.isNotBlank(" "));    //false
        System.out.println("---------------------------------------------");
        //去掉字符串两端的控制符(control characters, char <= 32) 
        System.out.println(StringUtils.trim(""));    //""
        System.out.println(StringUtils.trim(null));    //null
        System.out.println(StringUtils.trim(" "));    //""
        System.out.println(StringUtils.trim("  d  s "));    //"d  s"
        System.out.println("---------------------------------------------");
        //去掉两端的空符,如果为null或者为""或者为"  "均返回null
        System.out.println(StringUtils.trimToNull(null));    //null
        System.out.println(StringUtils.trimToNull(""));        //null
        System.out.println(StringUtils.trimToNull(" "));    //null
        System.out.println("---------------------------------------------");
        //去掉两端的空符,如果为null或者为""或者为"  "均返回""
        System.out.println(StringUtils.trimToEmpty("").equals(""));  //true
        System.out.println(StringUtils.trimToEmpty(null).equals("")); //true
        System.out.println(StringUtils.trimToEmpty(" ").equals(""));  //true
        System.out.println("---------------------------------------------");
        //判断两个字符串是否相等
        System.out.println(StringUtils.equals("",""));    //true
        System.out.println(StringUtils.equals(null,null));    //true
        System.out.println(StringUtils.equals(null,""));    //false
        System.out.println("---------------------------------------------");
        //返回字符searchChar在字符串str中第一次出现的位置。如果不存在返回-1 
        System.out.println(StringUtils.indexOf("abcdecc","c"));  //2
        System.out.println(StringUtils.indexOf("abcdecc","x"));  //-1
        System.out.println("---------------------------------------------");
        //判断字符串str中是否包含字符searchChar。 如果str为null或"",返回false; 如果searchChar不在str中,返回false。 
        System.out.println(StringUtils.contains("","a"));  //false 
        System.out.println(StringUtils.contains(null,"a"));    //false
        System.out.println("---------------------------------------------");
        //字符串截取
        System.out.println(StringUtils.substring("",5));  //""
        System.out.println(StringUtils.substring(null,5));        //true
        /*表示从第5位可是截取到末尾*/
        System.out.println(StringUtils.substring("abcdefjhi",5));  //"fjhi"
        System.out.println(StringUtils.substring("abcdef",1,3));   //bc
        //去掉字符串中包含remove的部分
        System.out.println(StringUtils.remove("abcde","bc"));  //ade
    }
}

本节完

转载于:https://www.cnblogs.com/wyw-action/articles/7001613.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值