Java技术升华Java日常学习记录

StringUtils学习笔记

2018-12-02  本文已影响167人  Waldeinsamkeit4

一、StringUtils字符串工具类

1.1.判断是否空字符串

字符串判空示例代码

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

说明:isBlankisEmpty的区别在于在于空格字符串的判断不同;简言之,Empty认为空格字符不是空字符串

1.2.判断是否存在空字符串

判断是否存在空字符串示例代码

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(new String[] {})  = false
StringUtils.isAnyBlank(new String[]{""}) = true
StringUtils.isAnyBlank("foo", "bar")     = false

说明:同理,这里的区别也是在于空格字符串的不同对待;

1.3.去除字符串前后空格

去除字符串前后空格示例代码

StringUtils.trim(null)                = null
StringUtils.trim("     ")             = ""
StringUtils.trimToNull(null)          = null
StringUtils.trimToNull("     ")       = null
StringUtils.trimToEmpty(null)         = ""
StringUtils.trimToEmpty("     ")      = ""

1.4.字符串截取

字符串截取示例代码

StringUtils.truncate(null, 0)       = null
StringUtils.truncate(null, 2)       = null
StringUtils.truncate("", 4)         = ""
StringUtils.truncate("abcdefg", 4)  = "abcd"
StringUtils.truncate(null, 0, 0) = null
StringUtils.truncate(null, 2, 4) = null
StringUtils.truncate("", 0, 10) = ""
StringUtils.truncate("", 2, 10) = ""
StringUtils.truncate("abcdefghij", 0, 3) = "abc"
StringUtils.truncate("abcdefghij", 5, 6) = "fghij"
StringUtils.truncate("abcdefghijklmno", -1, 10) = throws an IllegalArgumentException
StringUtils.truncate("abcdefghijklmno", Integer.MIN_VALUE, 10) = "abcdefghij"
StringUtils.truncate("abcdefghijklmno", 0, Integer.MAX_VALUE) = "abcdefghijklmno"

说明:更多的示例代码可以参考StringUtils源码;

1.5.字符串的剥离

字符串的剥离示例代码

StringUtils.strip("abcde", "de");  = abc
StringUtils.strip("abcde", "ed");  = abc
StringUtils.stripStart("deabced", "ed");  = abcd
StringUtils.stripStart("deabced", "de");  = abcd
StringUtils.stripEnd("  abcyx", "xyz");   = "  abc"
StringUtils.stripEnd("120.00", ".0");     = "12"
StringUtils.stripAll(["abc", "  abc"]);   = ["abc", "abc"]
StringUtils.stripAll(["yabcz", null], "yz");  = ["abc", null]

1.6.字符串的比较

字符串的比较代码示例

StringUtils.equals("abc", null)  = false
StringUtils.equals("abc", "abc") = true
StringUtils.equals("abc", "ABC") = false
tringUtils.equalsIgnoreCase("abc", "ABC") = true
StringUtils.compare("abc", "abc") = 0
StringUtils.compare("a", "b")     > 0
StringUtils.compare("b", "a")     < 0
StringUtils.compare("a", "B")     < 0
StringUtils.compare("ab", "abc")  < 0
StringUtils.compareIgnoreCase("a", "B")     < 0
StringUtils.compareIgnoreCase("A", "b")     < 0
StringUtils.compareIgnoreCase("ab", "ABC")  < 0
StringUtils.equalsAny("abc", "abc", "def") = true
StringUtils.equalsAny("abc", "ABC", "DEF") = false
StringUtils.equalsAnyIgnoreCase("abc", "abc", "def") = true
StringUtils.equalsAnyIgnoreCase("abc", "ABC", "DEF") = true

1.7.字符串的查找

字符串的查找示例代码

StringUtils.indexOf("aabaabaa", 'b') = 2
StringUtils.indexOf("aabaabaa", "")   = 0
StringUtils.indexOf("aabaabaa", "b", 3)  = 5
StringUtils.ordinalIndexOf("abababab", "abab", 3) = 4
StringUtils.lastIndexOf("aabaabaa", 'b') = 5
StringUtils.lastIndexOf("aabaabaa", 'a', 0)  = 0

1.8.是否包含字符串

是否包含字符串示例代码

StringUtils.contains("abc", 'a') = true
StringUtils.contains("abc", 'z') = false
StringUtils.containsIgnoreCase("abc", "A") = true
StringUtils.containsIgnoreCase("abc", "Z") = false
StringUtils.containsWhitespace("abc")      = false
StringUtils.containsWhitespace("a  bc")    = true
StringUtils.containsAny("zzabyycdxx",['z','a']) = true
StringUtils.containsAny("zzabyycdxx",['b','y']) = true
StringUtils.containsAny("zzabyycdxx",['z','y']) = true
StringUtils.containsAny("aba", ['z'])           = false
StringUtils.containsOnly("abab", 'abc') = true
StringUtils.containsOnly("ab1", 'abc')  = false
StringUtils.containsOnly("abz", 'abc')  = false
StringUtils.containsNone("abab", 'xyz') = true
StringUtils.containsNone("ab1", 'xyz')  = true
StringUtils.containsNone("abz", 'xyz')  = false

1.9.截取字符串

截取字符串示例代码

StringUtils.substring("abc", 2)  = "c"
StringUtils.substring("abc", 4)  = ""
StringUtils.substring("abc", -2) = "bc"
StringUtils.substring("abc", -4) = "abc"
StringUtils.substring("abc", 2, 2)   = ""
StringUtils.substring("abc", -2, -1) = "b"
StringUtils.substring("abc", -4, 2)  = "ab"
StringUtils.left("abc", 2)   = "ab"
StringUtils.left("abc", 4)   = "abc"
StringUtils.right("abc", 2)   = "bc"
StringUtils.right("abc", 4)   = "abc"
StringUtils.mid("abc", 2, 4)   = "c"
StringUtils.mid("abc", 4, 2)   = ""
StringUtils.mid("abc", -2, 2)  = "ab"
StringUtils.substringBefore("abcba", "b") = "a"
StringUtils.substringBefore("abc", "c")   = "ab"
StringUtils.substringBefore("abc", "d")   = "abc"
StringUtils.substringAfter("abcba", "b") = "cba"
StringUtils.substringAfter("abc", "c")   = ""
StringUtils.substringAfter("abc", "d")   = ""

1.10.字符串分割

字符串分割示例代码

StringUtils.split("abc  def") = ["abc", "def"]
StringUtils.split(" abc ")    = ["abc"]
StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
StringUtils.split("a:b:c", '.')    = ["a:b:c"]
StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
StringUtils.split("ab:cd:ef", ":", 3); = [ab, cd, ef]
StringUtils.split("ab:cd:ef", ":", 2); = [ab:cd:ef]
StringUtils.split("ab:cd:ef", ":", 1); = [ab, cd:ef]
StringUtils.splitByCharacterTypeCamelCase("number5")    = ["number", "5"]
StringUtils.splitByCharacterTypeCamelCase("fooBar")     = ["foo", "Bar"]
StringUtils.splitByCharacterTypeCamelCase("foo200Bar")  = ["foo", "200", "Bar"]
StringUtils.splitByCharacterTypeCamelCase("ASFRules")   = ["ASF", "Rules"]

1.11.字符串连接(数组转字符)

字符串连接示例代码

StringUtils.join(["a", "b", "c"]) = "abc"
StringUtils.join([null, "", "a"]) = "a"
StringUtils.join(["a", "b", "c"], ';')  = "a;b;c"
StringUtils.join(["a", "b", "c"], null) = "abc"
StringUtils.join([null, "", "a"], ';')  = ";;a"

1.12.删除和替换字符串

删除和替换字符串示例代码

StringUtils.removeStart("www.domain.com", "www.")   = "domain.com"
StringUtils.removeStart("domain.com", "www.")       = "domain.com"
StringUtils.removeStart("www.domain.com", "domain") = "www.domain.com"
StringUtils.removeStart("abc", "")    = "abc"
StringUtils.deleteWhitespace("abc")        = "abc"
StringUtils.deleteWhitespace("   ab  c  ") = "abc"
StringUtils.removeEnd("www.domain.com", ".com.")  = "www.domain.com"
StringUtils.removeEnd("www.domain.com", ".com")   = "www.domain"
StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com"
StringUtils.remove("queued", "ue") = "qd"
StringUtils.remove("queued", "zz") = "queued"
StringUtils.removeAll("any", ".*")  = ""
StringUtils.removeAll("any", ".+")  = ""
StringUtils.removeAll("abc", ".?")  = ""
StringUtils.removeAll("A&lt;__&gt;\n&lt;__&gt;B", "&lt;.*&gt;")      = "A\nB"
StringUtils.removeAll("A&lt;__&gt;\n&lt;__&gt;B", "(?s)&lt;.*&gt;")  = "AB"
StringUtils.removeAll("ABCabc123abc", "[a-z]")     = "ABC123"
StringUtils.replaceOnce("aba", "a", null)  = "aba"
StringUtils.replaceOnce("aba", "a", "")    = "ba"
StringUtils.replaceOnce("aba", "a", "z")   = "zba"
StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "_")  = "ABC_123"
StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "")   = "ABC123"
StringUtils.replaceAll("abc", "", "ZZ")  = "ZZaZZbZZcZZ"

1.13.重复字符串

重复字符串生成示例代码

StringUtils.repeat("a", 3)  = "aaa"
StringUtils.repeat("ab", 2) = "abab"
StringUtils.repeat("a", -2) = ""

1.14.字符串出现次数

字符串出现次数示例代码

StringUtils.countMatches("abba", "a")   = 2
StringUtils.countMatches("abba", "ab")  = 1

二、StringUtils其他常用方法

2.1.判断是否是空格或者数字

StringUtils.isNumericSpace("12 3") = true
StringUtils.isNumericSpace("12.3") = false

2.2.判断是否是空格

StringUtils.isWhitespace("")     = true
StringUtils.isWhitespace("  ")   = true
StringUtils.isWhitespace("abc")  = false

2.3.判断是否全是小写

StringUtils.isAllLowerCase("abc")  = true
StringUtils.isAllLowerCase("abC")  = false

2.4.判断是否全是大写

StringUtils.isAllUpperCase("ABC")  = true
StringUtils.isAllUpperCase("aBC")  = false

2.5.反转字符串

StringUtils.reverse("bat") = "tab"

2.6.字符串显示省略号

StringUtils.abbreviate("abcdefg", 6) = "abc..."
StringUtils.abbreviate("abcdefg", 7) = "abcdefg"
StringUtils.abbreviate("abcdefg", 8) = "abcdefg"
StringUtils.abbreviate("abcdefg", 4) = "a..."

2.7.判断字符串是否以什么开头

StringUtils.startsWith("abcdef", null)  = false
StringUtils.startsWith("abcdef", "abc") = true
StringUtils.startsWith("ABCDEF", "abc") = false

2.7.判断字符串是否以什么结尾

StringUtils.endsWith("ABCDEF", "def") = false
StringUtils.endsWith("ABCDEF", "cde") = false
StringUtils.endsWith("ABCDEF", "")    = true
上一篇下一篇

猜你喜欢

热点阅读