Java工具类StringUtils常用方法

2020-08-03  本文已影响0人  凯睿看世界

StringUtils类是org.apache.commons.lang3.StringUtils中的类,因此使用是需要引入
comons.lang包的。

StringUtils类中的方法都是static静态类方法的,
常用方法

isEmpty(String str) 判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0
StringUtils.isEmpty(null)          = true
StringUtils.isEmpty("")       = true
StringUtils.isEmpty(" ")      = false
StringUtils.isEmpty("        ")     = false
StringUtils.isEmpty("bob")       = false
StringUtils.isEmpty(" bob ") = false
isNotEmpty(String str)判断某字符串是否非空,等于!isEmpty(String str)
StringUtils.isNotEmpty(null)        = false
StringUtils.isNotEmpty("")           = false
StringUtils.isNotEmpty(" ")      = true
StringUtils.isNotEmpty("         ")    = true
StringUtils.isNotEmpty("bob")   = true
StringUtils.isNotEmpty(" bob ")   = true
isBlank(String str) 判断某字符串是否为空或长度为0或由空白符(whitespace)构成
StringUtils.isBlank(null)         = true
StringUtils.isBlank("")            = true
StringUtils.isBlank(" ")                 = true
StringUtils.isBlank("        ")          = true
StringUtils.isBlank("\t \n \f \r")    = true
StringUtils.isBlank("\b")               = false
StringUtils.isBlank("bob")            = false
StringUtils.isBlank(" bob ")        = false
contains(String str,char searchChar)str是原始字符串,searchChar是想要搜索的字符,此方法是检查字符串str中是否包含字符searchChar,如果str为null或””,则返回false。
contains(String str,String searchStr)str是原始字符串,searchStr是待搜索的字符串,此方法是检查字符串str中是否包含字符串searchStr,如果str为null,则返回false。
containsIgnoreCase(String str,String searchStr)在字符串str中搜索字符串searchStr,忽略大小写。如果str为Null,则返回false。
trimToNull(String str)移除字符串中字符的char<32(ASCII<32)的字符,如果该字符串是null或empty(“”),则返回null。
trimToEmpty(String str)移除字符串中字符的char<32(ASCII<32)的字符,如果该字符串是null或empty(“”),则返回empty(“”)
replace(String text,String searchString,String replacement)用一个字符串替换另一个字符串中出现的指定字符串
join(Object[] array,String separator);用指定分隔符连接数组各个元素。
endsWith(String str, String suffix)以suffix结尾的
StringUtils.substringAfter();截取某字符串指定字符串之后的内容。
StringUtils.countMatches(context, keyword);统计字符串匹配个数
StringUtils.contains(null, *)    = false
StringUtils.contains("", *)      = false
StringUtils.contains("abc", 'a') = true
StringUtils.contains("abc", 'z') = false

StringUtils.contains(null, *)     = false
StringUtils.contains(*, null)     = false
StringUtils.contains("", "")      = true
StringUtils.contains("abc", "")   = true
StringUtils.contains("abc", "a")  = true
StringUtils.contains("abc", "z")  = false

String eq = "合上#2炉B送风机电机ⅡB-14开关控制小开关。";
List<String> sList = new ArrayList();
sList.add("#2炉B送风机电机ⅡB-14开关");
sList.add("#2");
for(String s:sList){
    if(StringUtils.contains(eq,s)){
         System.out.println("包含:"+s);
    }
    /*if(eq.indexOf(s)!=-1){
     System.out.println("包含:"+s);
    }*/
}

StringUtils.contains(null, *) = false
StringUtils.contains(*, null) = false
StringUtils.contains("", "") = true
StringUtils.contains("abc", "") = true
StringUtils.contains("abc", "a") = true
StringUtils.contains("abc", "z") = false
StringUtils.contains("abc", "A") = true
StringUtils.contains("abc", "Z") = false

StringUtils.isBlank(null)      = true
StringUtils.isBlank("")        = true
StringUtils.isBlank(" ")       = true
StringUtils.isBlank("bob")     = false
StringUtils.isBlank("  bob  ") = false

StringUtils.trimToNull(null)          = null
StringUtils.trimToNull("")            = null
StringUtils.trimToNull("     ")       = null
StringUtils.trimToNull("abc")         = "abc"
StringUtils.trimToNull("    abc    ") = "abc"
StringUtils.trimToNull("    a b c    ") = "a b c"

StringUtils.trimToEmpty(null)          = ""
StringUtils.trimToEmpty("")            = ""
StringUtils.trimToEmpty("     ")       = ""
StringUtils.trimToEmpty("abc")         = "abc"
StringUtils.trimToEmpty("    abc    ") = "abc"

StringUtils.replace(null, *, *)        = null
 StringUtils.replace("", *, *)          = ""
 StringUtils.replace("any", null, *)    = "any"
 StringUtils.replace("any", *, null)    = "any"
 StringUtils.replace("any", "", *)      = "any"
 StringUtils.replace("aba", "a", null)  = "aba"
 StringUtils.replace("aba", "a", "")    = "b"
 StringUtils.replace("aba", "a", "z")   = "zbz"

这样做的操作Stringnull安全的。

StringUtils类定义与字符串处理某些词。

StringUtils``null静静地处理输入字符串。这就是说,一个null输入将返回null。在哪里booleanint正在退货的细节因方法而异。

处理的一个副作用null是a NullPointerException应该被认为是一个错误 StringUtils(除了被弃用的方法)。

这个类中的方法给出了示例代码来解释它们的操作。该符号*用于指示包括的任何输入null

参考:
https://www.4spaces.org/java-stringutils/
https://blog.csdn.net/baidu_31071595/article/details/51320622
https://www.cnblogs.com/yccmelody/p/8117318.html
https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html

上一篇 下一篇

猜你喜欢

热点阅读