栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java的StringUtils.isBlank和StringUtils.isEmpty方法区别(org.apache.commons.lang3.StringUtils)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java的StringUtils.isBlank和StringUtils.isEmpty方法区别(org.apache.commons.lang3.StringUtils)

前言

估计很多朋友跟我一样,平时也不会特别去注意究竟用isBlank还是isEmpty去判断空字符串,但是大部分场景优先使用isBlank就对了。

  • isEmpty是否为空,只有当==null或者==""才为空
  • isBlank是否为真空,==null和==""以及各种长度的空格==" "都为空

而且除了isEmpty/isNotEmpty/isNotBlank/isBlank外,其实还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank让我们一起来研究一下org.apache.commons.lang3.StringUtils这个工具类吧。

isBank()

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

- StringUtils.isBlank(null) = true
- StringUtils.isBlank("") = true
- StringUtils.isBlank(" ") = true
- StringUtils.isBlank("                ") = true
- StringUtils.isBlank("moshow") = false
- StringUtils.isBlank("   moshow  ") = false
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, " ") = true
StringUtils.isAnyBlank("", " ") = true
 
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, " ") = false
StringUtils.isNoneBlank("", " ") = false
StringUtils.isNoneBlank("moshow", " moshow ") = true

关于其他方法其实都是以此类推,知道这几个,就可以类推对应的is*Empty等等鞥,这里就不做重复展示。

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

方法 (整理 by https://zhengkai.blog.csdn.net/ )说明EN说明CN
IsEmpty/IsBlankchecks if a String contains text判断是否包含文本
Trim/Stripremoves leading and trailing whitespace删除多余空格
Equals/Comparecompares two strings in a nullsafe manner
startsWith /endsWithcheck if a String starts/ends with a prefix判断是什么开头/结尾
IndexOf/LastIndexOf/Containscontain a string and return the index判断字符串在string中的位置,判断是否包含
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyButindex of any of a set of Strings如果包含某几个字符串就返回
ContainsOnly/ContainsNone/ContainsAnychecks if String contains only/none/any of these characters
Substring/Left/Right/Mid/SubstringBefore/SubstringAfter/SubstringBetweensafe substring extractions截取字符串方法
Split/Joinsplits a String into an array of substrings and vice versa切割或者拼接字符串
Remove/Deleteremoves part of a String移除
Replace/OverlaySearches a String and replaces one String with another替换
Chomp/Chopremoves the last part of a String删除字符串的最后一部分
AppendIfMissing/PrependIfMissingappends a suffix to the start/end of the String if not present前缀后缀
LeftPad/RightPad/Center/Repeatpads a String填充字符串
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalizechanges the case of a String大写/小写/交换大小写/大写/取消大写
CountMatchescounts the number of occurrences of one String in another统计匹配
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintablechecks the characters in a String是否字母/数字/空格/Ascii
DefaultStringprotects against a null input String防止空输入字符串
Rotaterotate (circular shift) a String旋转(循环移位)一个字符串
Reverse/ReverseDelimitedreverses a String反转字符串
Abbreviateabbreviates a string using ellipses or another given String使用省略号或另一个给定的字符串缩写字符串
Differencecompares Strings and reports on their differences比较字符串并报告它们的差异
LevenshteinDistancethe number of changes needed to change one String into another将一个字符串更改为另一个字符串所需的更改次数
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306382.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号