StringUtils是org.apache.commons.lang jar包里面的类方法,当输入参数String为null则不会抛出NullPointerException,而是做了相应处理,null是安全的。而JDK中String类里面的方法会抛出NullPointerException。下面我介绍StringUtils一些常用的方法,它和String类的方法有很多相似的地方。开发的时候用StringUtils的jar才可以用这个类里面的方法,其实StringUtils类的方法也可以用String类里面方法实现,开发的时候根据需要来选择用哪一类的方法吧。

1.public static boolean isEmpty(String str)

判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0。但是字符串为空格为true。

StringUtils.isEmpty(null)      = true

StringUtils.isEmpty("")        = true

StringUtils.isEmpty(" ")       = false

StringUtils.isEmpty("bob")     = false

StringUtils.isEmpty("  bob  ") = false

源码:

public static boolean isEmpty(String str) {
return str == null || str.length() == 0; }

2.public static boolean isNotEmpty(String str)

判断某字符串是否不为空,与isEmpty(String str)相反。

3.public static boolean isBlank(String str)

判断字符串是否为空,字符长度为0同时也包括空白符的字符串,这个比isEmpty范围更广。

StringUtils.isBlank(null)      = true

StringUtils.isBlank("")        = true

StringUtils.isBlank(" ")       = true

StringUtils.isBlank("bob")     = false

StringUtils.isBlank("  bob  ") = false

源码:

public static boolean isBlank(String str) {

         int strLen;

         if (str == null || (strLen = str.length()) == 0) {

             return true;

          }

          for (int i = 0; i < strLen; i++) {

              if ((Character.isWhitespace(str.charAt(i)) == false)) {

                   return false;

               }

           }

           return true;

       }

改写源码方法:

public static boolean isBlank(String str) {

        if (str == null || ( str.trim().length()) == 0) {

            return true;

         }

       return false;

       /*int strLen;

        if (str == null || (strLen = str.length()) == 0) {

            return true;

         }

         for (int i = 0; i < strLen; i++) {

             if ((Character.isWhitespace(str.charAt(i)) == false)) {

                  return false;

              }

          }

          return true;*/

      }

4. public static boolean isNotBlank(String str)

判断字符串是否不为空,字符长度不为0同时不为空白符的字符串,与isBlank(String str) 方法相反。

5. public static String trim(String str)

去掉字符串两端的控制符(char <= 32),如果输入为null则返回null。注意:字符串中间有控制符则不会去掉,trim()方法在String里面也有这个方法,但是两者有一定的区别,当对null进行相关操作的话,StringUtils.trim(null)会返回为null,但是调用null.trim()则会抛出java.lang.NullPointerException异常。

StringUtils.trim(null)          = null

StringUtils.trim("")            = ""

StringUtils.trim("     ")       = ""

StringUtils.trim("    \b \t \n \f \r    ") = ""

StringUtils.trim("abc")         = "abc"

StringUtils.trim("abc   def")  = "abc   def"

StringUtils.trim("    abc    ") = "abc"

源码:

public static String trim(String str) {

return str == null ? null : str.trim();

}

6.public static String strip(String str)

去掉字符串两端的空白符(whitespace),如果变为null,则返回null

7. public static String swapCase(String str)

把字符串中的字符大写转换为小写,小写转换为大写。

StringUtils.swapCase(null)                 = null

StringUtils.swapCase("")                   = ""

StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"

StringUtils一些常用方法的更多相关文章

  1. StringUtils 的常用方法

    StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...

  2. org.apache.commons.lang.StringUtils的常用方法

    org.apache.commons.lang.StringUtils是apache的commons-lang-x.x.jar下的包,里面包含很多字符串操作方法, 官网(http://commons. ...

  3. StringUtils类常用方法介绍

    StringUtils方法包的位置:org.apache.commons.lang.StringUtils StringUtils 方法的操作对象是 java.lang.String 类型的对象,是 ...

  4. 关于StringUtils的常用方法

    StringUtils.split(String, char) * <pre> * StringUtils.split(null, *) = null * StringUtils.spli ...

  5. StringUtils中常用方法leftPad(),rightPad(),center()

    org.apache.commons.lang3的StringUtils 方法如下: public static String leftPadTime(Integer time){    return ...

  6. StringUtils工具类的常用方法

    StringUtils 方法的操作对象是 java.lang.String 类型的对象,是对 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String ...

  7. StringUtils工具类常用方法详解

    StringUtils 常用方法 1.isEmpty(String str) 是否为空,空格字符为false2.isNotEmpty(String str) 是否为非空,空格字符为true3.isBl ...

  8. StringUtils常用方法+StringUtils详细介绍

    StringUtils常用方法+StringUtils详细介绍   StringUtils用法+StringUtils详细介绍博文来源:http://yijianfengvip.blog.163.co ...

  9. StringUtils常用方法介绍

    要使用StringUtils类,首先需要导入:import org.apache.commons.lang.StringUtils;这个包 在maven项目中需要添加下面这个依赖: <depen ...

随机推荐

  1. 【转】MPU6050的数据获取、分析与处理

    摘要 MPU6050是一种非常流行的空间运动传感器芯片,可以获取器件当前的三个加速度分量和三个旋转角速度.由于其体积小巧,功能强大,精度较高,不仅被广泛应用于工业,同时也是航模爱好者的神器,被安装在各 ...

  2. Oracle数据库--PL/SQL存储过程和函数的建立和调用

    1.存储过程建立的格式: create or replace procedure My_Procedure is begin --执行部分(函数内容); end; / 例子:(以hr表为例) crea ...

  3. adb logcat介绍

    logcat命令语法: [adb] logcat [<option>] ... [<filter-spec>] ... adb logcat -c 清除所有以前的日志 adb ...

  4. javascript中的this到底指什么?

    来自百度知道解释 JavaScript:this是什么? 定义:this是包含它的函数作为方法被调用时所属的对象.说明:这句话有点咬嘴,但一个多余的字也没有,定义非常准确,我们可以分3部分来理解它!1 ...

  5. 安装Python和Anaconda

    安装Python和Anaconda 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装anaconda anaconda包括了Python的集成开发环境. 1.打开下载的网站 ...

  6. python---django中form组件(1)简单使用和字段了解

    Django中的Form组件功能: 1.对用户请求的验证 2.生成html代码 Form使用:对用户请求进行验证 前端代码: <form action="/f1.html" ...

  7. 蓝桥杯 算法提高 9-3摩尔斯电码 _c++ Map容器用法

    //****|*|*-**|*-**|--- #include <iostream> #include <map> #include <vector> #inclu ...

  8. github 远程仓库

    因为本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以设置一下 第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_r ...

  9. JMS学习(二)之ActiveMQ

    1,ActiveMQ是Apache实现的基于JMS的一个消息服务器.下面记录ActiveMQ的一些基本知识. 2,ActiveMQ connectors:ActiveMQ providesconnec ...

  10. 基于I2C总线的0.96寸OLED显示屏驱动

    资料未整理,先占位置,以后补充