StringUtils的具体操作

2023-11-27 20:50
文章标签 具体操作 stringutils

本文主要是介绍StringUtils的具体操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我是 ABin-阿斌:写一生代码,创一世佳话,筑一览芳华。 如果小伙伴们觉得文章有点 feel ,那就点个赞再走哦。
在这里插入图片描述

原文链接:https://sourl.cn/dRpJ6b

一、前言

  • 在日常开发当中我们会使用很多的判空工具进行校验,今天我们就看看对字符串判空有哪些骚操作
  • StringUtils 是我们比较常用的工具类了,也许你两个都不知道,也许你除了isEmpty/isNotEmpty/isNotBlank/isBlank外并不知道还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank的存在。 那么,接下来让我们一起来探索org.apache.commons.lang3.StringUtils;这个工具类。

二、正文

1、isEmpty 系列

1.1、StringUtils.isEmpty()

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

1.2、StringUtils.isNotEmpty()

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

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

1.3、StringUtils.isAnyEmpty()

  • 是否有一个为空,只有一个为空,就为 true。
StringUtils.isAnyEmpty(null) = trueStringUtils.isAnyEmpty(null, "foo") = trueStringUtils.isAnyEmpty("", "bar") = trueStringUtils.isAnyEmpty("bob", "") = trueStringUtils.isAnyEmpty(" bob ", null) = trueStringUtils.isAnyEmpty(" ", "bar") = falseStringUtils.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;
}

1.4、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) {

2、isBank 系列

2.1、StringUtils.isBlank()

  • 是否为真空值(空格或者空值)
StringUtils.isBlank(null) = trueStringUtils.isBlank("") = trueStringUtils.isBlank(" ") = trueStringUtils.isBlank("bob") = falseStringUtils.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;
}

2.2、StringUtils.isNotBlank()

  • 是否真的不为空,不是空格或者空值 ,相当于!isBlank();
public static boolean isNotBlank(final CharSequence cs) {return !isBlank(cs);}

2.3、StringUtils.isAnyBlank()

  • 是否包含任何真空值(包含空格或空值)
StringUtils.isAnyBlank(null) = trueStringUtils.isAnyBlank(null, "foo") = trueStringUtils.isAnyBlank(null, null) = trueStringUtils.isAnyBlank("", "bar") = trueStringUtils.isAnyBlank("bob", "") = trueStringUtils.isAnyBlank(" bob ", null) = trueStringUtils.isAnyBlank(" ", "bar") = trueStringUtils.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;
}

2.4、StringUtils.isNoneBlank()

  • 是否全部都不包含空值或空格
StringUtils.isNoneBlank(null) = falseStringUtils.isNoneBlank(null, "foo") = falseStringUtils.isNoneBlank(null, null) = falseStringUtils.isNoneBlank("", "bar") = falseStringUtils.isNoneBlank("bob", "") = falseStringUtils.isNoneBlank(" bob ", null) = falseStringUtils.isNoneBlank(" ", "bar") = falseStringUtils.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);
}

3、StringUtils 的其他方法

  • 可以参考官方的文档,里面有详细的描述,有些方法还是很好用的。 StringUtils官方文档
方法名英文解释中文解释
IsEmpty/IsBlankchecks if a String contains text检查字符串是否包含文本
Trim/Stripremoves leading and trailing whitespace删除前导和尾随空格
Equals/Comparecompares two strings null-safe比较两个字符串是否为 null 安全的
startsWithcheck if a String starts with a prefix null-safe检查字符串是否以前缀 null 安全开头
endsWithcheck if a String ends with a suffix null-safe检查字符串是否以后缀 null 安全结尾
IndexOf/LastIndexOf/Containsnull-safe index-of checks包含空安全索引检查
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyButindex-of any of a set of Strings任意一组字符串的索引
ContainsOnly/ContainsNone/ContainsAnydoes String contains only/none/any of these characters字符串是否仅包含/无/这些字符中的任何一个
Substring/Left/Right/Midnull-safe substring extractions字符串安全提取
SubstringBefore/SubstringAfter/SubstringBetweensubstring extraction relative to other strings相对其他字符串的字符串提取
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删除字符串的最后一部分
AppendIfMissingappends a suffix to the end of the String if not present如果不存在后缀,则在字符串的末尾附加一个后缀
PrependIfMissingprepends a prefix to the start 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检查字符串中的字符
DefaultStringprotects against a null input String防止输入字符串为空
Rotaterotate (circular shift) a String旋转(循环移位)字符串
Reverse/ReverseDelimitedreverses a String反转字符串
Abbreviateabbreviates a string using ellipsis or another given String使用省略号或另一个给定的 String 缩写一个字符串
Differencecompares Strings and reports on their differences比较字符串并报告其差异
LevenshteinDistancethe number of changes needed to change one String into another将一个 String 转换为另一个 String 所需的更改次数

这篇关于StringUtils的具体操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/428447

相关文章

项目中的工具类StringUtils和DigestUtils

在项目中导入:commons-codec.jar和commons-lang.jar二个架包。 <span style="font-size:18px;">package junit;import org.apache.commons.codec.digest.DigestUtils;import org.apache.commons.lang.StringUtils;import or

Apache服务器的配置具体操作—— Apache比较强大,所以要配置Apache作为本地web服务器

配置Apache的详细步骤如下: 想打开整个系统的隐藏文件可以在终端下输入以下命令defaults write com.apple.finder AppleShowAllFiles -bool true关闭显示隐藏功能defaults write com.apple.finder AppleShowAllFiles -bool false安装Apache(Mac10.10)需要做的

docker具体操作

安装docker :  前提  centos 7  64位  centos内核版本3.10以上  1    uname -r  查看内核    2  yum install -y   yum-utils  device-mapper-persistent-data   lvm2  下载依赖包  3 um-config-manager --add-repo http://mirrors.

服装门店神秘顾客暗访具体操作流程

神秘顾客,可以帮助企业了解各种类型窗口行业营业/服务的环境、服务人员的服务态度、业务素质和技能等情况,广泛应用到如电信、银行、超市、连锁店、医院等窗口服务性行业。从企业的长远发展来考虑,做神秘顾客调查是非常重要的。开元研究,成立于2002年,是一家专业的市场调研公司。具有全国范围内实施神秘客户访谈执行能力,访客遍布在国内任何城市。以下则是其服装门店神秘顾客调查方案部分方案设计。 具体操作流程

ERROR----java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

2013-4-28 13:17:57 org.apache.catalina.core.StandardContext filterStart 严重: Exception starting filter struts2 java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

Java高级个人笔记(StringUtils工具类用法)

/*1.字符串以prefix开始*/StringUtils.startsWith("sssdf","");//结果是:trueStringUtils.startsWith("sssdf","");//结果是:trueStringUtils.startsWith("sssdf","s");//结果是:trueStringUtils.startsWith("sssdf","ss");//结果是

java StringUtils类常用方法

StringUtils类是Apache Commons Lang库中提供的一个工具类,用于处理字符串操作。它包含了许多常用的方法,以下是其中一部分常用方法: StringUtils.isEmpty(String str):判断字符串是否为空,如果字符串为null、空字符串或只包含空格,则返回true。 StringUtils.isNotBlank(String str):判断字符串是否不为空

Git大文件无法直接push用git lfs track 上传大文件具体操作

Git 因为大文件push失败 回退到git add前用git lfs track单独添加大文件 以下work flow仅代表个人解决问题的办法,有优化流程的欢迎交流 回退到git add前 以下指令回退一个commit git reset --soft HEAD~1 以下指令撤销所有git add操作,但不删除本地修改 git reset HEAD . 用git

[Java基础]计算字符串数组内数组总长 (StringUtils.join StringBuilder.append)

前言 最近在开发的时候, 需要计算一个String数组, 拼接后的长度. 本来是准备自己写了一个简单的工具类, 计算长度. 经过同事的提醒, 发现还有这样一个好用的工具类. ​ 方法一 public static int calculateStrJoinLengthOfListMethod1(List<String> strList){if(CollectionUtils.isE

StringUtils 中isEmpty 和 isBlank

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