详谈StringUtils3之StringUtils.isEmpty()和StringUtils.isB的区别

目录
  • #isEmpty系列
    • StringUtils.isEmpty()
    • StringUtils.isNotEmpty()
    • StringUtils.isAnyEmpty()
    • StringUtils.isNoneEmpty()
  • #isBank系列
    • StringUtils.isBlank()
    • StringUtils.isNotBlank()
    • StringUtils.isAnyBlank()
    • StringUtils.isNoneBlank()
  • StringUtils的其他方法

也许你两个都不知道,也许你除了isEmpty/isNotEmpty/isNotBlank/isBlank外,并不知道还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank的存在, come on ,让我们一起来探索org.apache.commons.lang3.StringUtils;这个工具类.

#isEmpty系列

StringUtils.isEmpty()

>>>是否为空. 可以看到 " " 空格是会绕过这种空判断,因为是一个空格,并不是严格的空值,会导致 isEmpty(" ")=false

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty(“bob”) = false
StringUtils.isEmpty(" bob ") = false
    /**
     *
     * <p>NOTE: This method changed in Lang version 2.0.
     * It no longer trims the CharSequence.
     * That functionality is available in isBlank().</p>
     *
     * @param cs  the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is empty or null
     * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
     */
    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

StringUtils.isNotEmpty()

>>>相当于不为空 , = !isEmpty()

public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

StringUtils.isAnyEmpty()

>>>是否有一个为空,只有一个为空,就为true.

StringUtils.isAnyEmpty(null) = true
StringUtils.isAnyEmpty(null, “foo”) = true
StringUtils.isAnyEmpty("", “bar”) = true
StringUtils.isAnyEmpty(“bob”, “”) = true
StringUtils.isAnyEmpty(" bob ", null) = true
StringUtils.isAnyEmpty(" ", “bar”) = false
StringUtils.isAnyEmpty(“foo”, “bar”) = false
	/**
     * @param css  the CharSequences to check, may be null or empty
     * @return {@code true} if any of the CharSequences are empty or null
     * @since 3.2
     */
    public static boolean isAnyEmpty(final CharSequence... css) {
      if (ArrayUtils.isEmpty(css)) {
        return true;
      }
      for (final CharSequence cs : css){
        if (isEmpty(cs)) {
          return true;
        }
      }
      return false;
    }

StringUtils.isNoneEmpty()

>>>相当于!isAnyEmpty(css) , 必须所有的值都不为空才返回true

    /**
     * <p>Checks if none of the CharSequences are empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isNoneEmpty(null)             = false
     * StringUtils.isNoneEmpty(null, "foo")      = false
     * StringUtils.isNoneEmpty("", "bar")        = false
     * StringUtils.isNoneEmpty("bob", "")        = false
     * StringUtils.isNoneEmpty("  bob  ", null)  = false
     * StringUtils.isNoneEmpty(" ", "bar")       = true
     * StringUtils.isNoneEmpty("foo", "bar")     = true
     * </pre>
     *
     * @param css  the CharSequences to check, may be null or empty
     * @return {@code true} if none of the CharSequences are empty or null
     * @since 3.2
     */
    public static boolean isNoneEmpty(final CharSequence... css) {
      return !isAnyEmpty(css);
    }

#isBank系列

StringUtils.isBlank()

>>> 是否为真空值(空格或者空值)

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank(“bob”) = false
StringUtils.isBlank(" bob ") = false
    /**
     * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
     * @param cs  the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is null, empty or whitespace
     * @since 2.0
     * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
     */
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

StringUtils.isNotBlank()

>>> 是否真的不为空,不是空格或者空值 ,相当于!isBlank();

public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

StringUtils.isAnyBlank()

>>>是否包含任何真空值(包含空格或空值)

StringUtils.isAnyBlank(null) = true
StringUtils.isAnyBlank(null, “foo”) = true
StringUtils.isAnyBlank(null, null) = true
StringUtils.isAnyBlank("", “bar”) = true
StringUtils.isAnyBlank(“bob”, “”) = true
StringUtils.isAnyBlank(" bob ", null) = true
StringUtils.isAnyBlank(" ", “bar”) = true
StringUtils.isAnyBlank(“foo”, “bar”) = false
     /**
     * <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only..</p>
     * @param css  the CharSequences to check, may be null or empty
     * @return {@code true} if any of the CharSequences are blank or null or whitespace only
     * @since 3.2
     */
    public static boolean isAnyBlank(final CharSequence... css) {
      if (ArrayUtils.isEmpty(css)) {
        return true;
      }
      for (final CharSequence cs : css){
        if (isBlank(cs)) {
          return true;
        }
      }
      return false;
    }

StringUtils.isNoneBlank()

>>>是否全部都不包含空值或空格

StringUtils.isNoneBlank(null) = false
StringUtils.isNoneBlank(null, “foo”) = false
StringUtils.isNoneBlank(null, null) = false
StringUtils.isNoneBlank("", “bar”) = false
StringUtils.isNoneBlank(“bob”, “”) = false
StringUtils.isNoneBlank(" bob ", null) = false
StringUtils.isNoneBlank(" ", “bar”) = false
StringUtils.isNoneBlank(“foo”, “bar”) = true
    /**
     * <p>Checks if none of the CharSequences are blank ("") or null and whitespace only..</p>
     * @param css  the CharSequences to check, may be null or empty
     * @return {@code true} if none of the CharSequences are blank or null or whitespace only
     * @since 3.2
     */
    public static boolean isNoneBlank(final CharSequence... css) {
      return !isAnyBlank(css);
    }

StringUtils的其他方法

可以参考官方的文档,里面有详细的描述,有些方法还是很好用的.

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

方法名 英文解释 中文解释
IsEmpty/IsBlank checks if a String contains text 检查字符串是否包含文本
Trim/Strip removes leading and trailing whitespace 删除前导和尾随空格
Equals/Compare compares two strings null-safe 比较两个字符串是否为null安全的
startsWith check if a String starts with a prefix null-safe 检查字符串是否以前缀null安全开头
endsWith check if a String ends with a suffix null-safe 检查字符串是否以后缀null安全结尾
IndexOf/LastIndexOf/Contains null-safe index-of checks 包含空安全索引检查
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut index-of any of a set of Strings 任意一组字符串的索引
ContainsOnly/ContainsNone/ContainsAny does String contains only/none/any of these characters 字符串是否仅包含/无/这些字符中的任何一个
Substring/Left/Right/Mid null-safe substring extractions 字符串安全提取
SubstringBefore/SubstringAfter/SubstringBetween substring extraction relative to other strings -相对其他字符串的字符串提取  
Split/Join splits a String into an array of substrings and vice versa 将字符串拆分为子字符串数组,反之亦然
Remove/Delete removes part of a String -删除字符串的一部分  
Replace/Overlay Searches a String and replaces one String with another 搜索字符串,然后用另一个字符串替换
Chomp/Chop removes the last part of a String 删除字符串的最后一部分
AppendIfMissing appends a suffix to the end of the String if not present 如果不存在后缀,则在字符串的末尾附加一个后缀
PrependIfMissing prepends a prefix to the start of the String if not present 如果不存在前缀,则在字符串的开头添加前缀
LeftPad/RightPad/Center/Repeat pads a String 填充字符串
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize changes the case of a String 更改字符串的大小写
CountMatches counts the number of occurrences of one String in another 计算一个字符串在另一个字符串中出现的次数
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable checks the characters in a String 检查字符串中的字符
DefaultString protects against a null input String 防止输入字符串为空
Rotate rotate (circular shift) a String 旋转(循环移位)字符串
Reverse/ReverseDelimited reverses a String -反转字符串  
Abbreviate abbreviates a string using ellipsis or another given String 使用省略号或另一个给定的String缩写一个字符串
Difference compares Strings and reports on their differences 比较字符串并报告其差异
LevenshteinDistance the number of changes needed to change one String into another 将一个String转换为另一个String所需的更改次数

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Java中StringUtils工具类进行String为空的判断解析

    判断某字符串是否为空,为空的标准是str==null或str.length()==0 1.下面是StringUtils判断是否为空的示例: StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理 StringUtils.isEmpty(" ") = fals

  • StringUtils中的isEmpty、isNotEmpty、isBlank和isNotBlank的区别详解

    一.StringUtils中的isEmpty方法 1.StringUtils中的isEmpty方法中的源码如下: 注:由源码可知(判断某字符串是否为空,为空的标准是str==null或str.length()==0) 2.StringUtils中的isEmpty方法示例,如下代码 package com.rf.designPatterns.singleton; import org.apache.commons.lang.StringUtils; /** * @description: * @a

  • StringUtils里的isEmpty方法和isBlank方法的区别详解

    前言 我们常说的字符串为空,其实就是一个没有字符的空数组.比如: String a = ""; a 就可以称为是一个空字符串.由于 String 在 Java 中底层是通过 char 数组去存储字符串的,所以空字符串对应的 char 数组表现形式为 private final char value[] = new char[0]; 但实际工作中,我们需要对字符串进行一些校验,比如:是否为 null,是否为空,是否去掉空格.换行符.制表符等也不为空.我们一般都是通过一些框架的工具类去做这

  • 详谈StringUtils3之StringUtils.isEmpty()和StringUtils.isB的区别

    目录 #isEmpty系列 StringUtils.isEmpty() StringUtils.isNotEmpty() StringUtils.isAnyEmpty() StringUtils.isNoneEmpty() #isBank系列 StringUtils.isBlank() StringUtils.isNotBlank() StringUtils.isAnyBlank() StringUtils.isNoneBlank() StringUtils的其他方法 也许你两个都不知道,也许你

  • 详谈Java中的Object、T(泛型)、?区别

    因为最近重新看了泛型,又看了些反射,导致我对Object.T(以下代指泛型).?产生了疑惑. 我们先来试着理解一下Object类,学习Java的应该都知道Object是所有类的父类,注意:那么这就意味着它的范围非常广!首先记住这点,如果你的参数类型时Object,那么的参数类型将非常广! <Thinking in Java>中说很多原因促成了泛型的出现,最引人注目的一个原因就是为了创造容器类.这个要怎么来理解呢?我的理解是,可以抛开这个为了创造容器类这个,而是回到泛型的目的是限定某种类型上来.

  • 详谈js中数组(array)和对象(object)的区别

    •object 类型: ◦ 创建方式: /*new 操作符后面Object构造函数*/ var person = new Object(); person.name = "lpove"; person.age = 21; /*或者用对象字面量的方法*/ var person = { name: "lpove"; age : 21; } •array类型 ◦ 创建方式: `var colors = new Array("red","blu

  • 详谈Java泛型中T和问号(通配符)的区别

    类型本来有:简单类型和复杂类型,引入泛型后把复杂类型分的更细了. 概述 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方法. Java语言引入泛型的好处是安全简单. 在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的"任意化","任意化"带来的缺点是要做显式的强制类型转换,而这种转换是要求开发者对

  • 详谈pandas中agg函数和apply函数的区别

    在利用python进行数据分析 这本书中其实没有明确表明这两个函数的却别,而是说apply更一般化. 其实在这本书的第九章'数组及运算和转换'点到了两者的一点点区别:agg是用来聚合运算的,所谓的聚合当然是合成的成分比较大些,这一节开头就点到了:聚合只不过是分组运算的其中一种而已.它是数据转换的一个特例,也就是说,它接受能够将一维数组简化为标量值的函数. 当然这两个函数都是作用在groupby对象上的,也就是分完组的对象上的,分完组之后针对某一组,如果值是一维数组,在利用完特定的函数之后,能做到

  • 详谈springboot过滤器和拦截器的实现及区别

    前言 springmvc中有两种很普遍的AOP实现: 1.过滤器(Filter) 2.拦截器(Interceptor) 本篇面对的是一些刚接触springboot的人群 所以主要讲解filter和interceptor的简单实现和它们之间到底有什么区别 (一些复杂的功能我会之后发出文章,请记得关注) Filter的简单实现 字面意思:过滤器就是过滤的作用,在web开发中过滤一些我们指定的url 那么它能帮我们过滤什么呢? 那功能可就多了: 比如过拦截掉我们不需要的接口请求 修改请求(reques

  • 详谈套接字中SO_REUSEPORT和SO_REUSEADDR的区别

    Socket的基本背景 在讨论这两个选项的区别时,我们需要知道的是BSD实现是所有socket实现的起源.基本上其他所有的系统某种程度上都参考了BSD socket实现(或者至少是其接口),然后开始了它们自己的独立发展进化.显然,BSD本身也是随着时间在不断发展变化的.所以较晚参考BSD的系统比较早参考BSD的系统多一些特性.所以理解BSD socket实现是理解其他socket实现的基石.下面我们就分析一下BSD socket实现. 在这之前,我们首先要明白如何唯一识别TCP/UDP连接.TC

  • 详谈Pandas中iloc和loc以及ix的区别

    Pandas库中有iloc和loc以及ix可以用来索引数据,抽取数据.但是方法一多也容易造成混淆.下面将一一来结合代码说清其中的区别. 1. iloc和loc的区别: iloc主要使用数字来索引数据,而不能使用字符型的标签来索引数据.而loc则刚好相反,只能使用字符型标签来索引数据,不能使用数字来索引数据,不过有特殊情况,当数据框dataframe的行标签或者列标签为数字,loc就可以来其来索引. 好,先上代码,先上行标签和列标签都为数字的情况. import pandas as pd impo

  • 详谈vue中router-link和传统a链接的区别

    Vue-router是伴随着Vue框架出现的路由系统,它也是公认的一种优秀的路由解决方案.在使用Vue-router时候,我们常常会使用其自带的路径跳转组件Link,通过实现跳转,这和传统的何其相似!但它们到底有什么具体的区别呢? 官方中给出的解释是这样的: <router-link> 比起写死的 <a href="..." rel="external nofollow" rel="external nofollow" >

随机推荐