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. 【设计模式】—— 职责链模式ChainOfResponsibility

    前言:[模式总览]——————————by xingoo 模式意图 避免请求的发送者,和接受者过度的耦合在一起.一个请求者只需要发送一个请求即可,它的请求具体由后面哪个对象进行响应,并不需要关心.而请 ...

  2. poj 2528 (线段树+特殊离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 51098   Accepted: 14788 ...

  3. MT【111】画图估计

    评:此类方程是超越方程,一般情况下无法解出具体的解,常见手段:1.画图  2.猜根.此处可以取特殊值a=2.5,b=3.5,容易知道此时$x=2.5\in(2,3)$

  4. [SDOI2009]Bill的挑战——全网唯一 一篇容斥题解

    全网唯一一篇容斥题解 Description Solution 看到这个题,大部分人想的是状压dp 但是我是个蒟蒻没想到,就用容斥切掉了. 并且复杂度比一般状压低, (其实这个容斥的算法,提出来源于y ...

  5. Redis连接的客户端(connected_clients)数过高或者不减的问题解决方案

    最近的项目上使用ServiceStack.Redis上了redis缓存,其中遇到了很多问题.. 比如说 某一天发现redis做的缓存竟然失效了,然后查了下日志  报错max number of cli ...

  6. Linux上常用的基本命令

    复制:copy [keysystem@localhost happydzy]$ cp file1 file2 [keysystem@localhost happydzy]$ ll total -rw- ...

  7. 【leetcode】 Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. 数据结构(三)串---KMP模式匹配算法实现及优化

    KMP算法实现 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include ...

  9. bzoj千题计划286:bzoj1226: [SDOI2009]学校食堂Dining

    http://www.lydsy.com/JudgeOnline/problem.php?id=1226 关键点:一个人只能忍受 ‘紧跟’ 在他 后面的b个人比他先打到饭 dp[i][j][k] 前i ...

  10. 流媒体技术学习笔记之(七)进阶教程OBS参数与清晰度流畅度的关系

    源码地址:https://github.com/Tinywan/PHP_Experience 很多主播问过OBS的参数到底什么影响画质,到底什么影响流畅度,那么本篇教程尽量用通俗的语言解释下一些重要参 ...