一,非空判断

  1. StringUtils.isEmpty(null) = true
  2. StringUtils.isEmpty("") = true
  3. StringUtils.isEmpty(" ") = false
  4. StringUtils.isEmpty("bob") = false
  5. ---------------------------------------
  6. StringUtils.isBlank(null) = true
  7. StringUtils.isBlank("") = true
  8. StringUtils.isBlank(" ") = true
  9. StringUtils.isBlank("bob") = false

二,匹配

1,是否相等

  1. //是否相等
  2. StringUtils.equals(null, null) = true
  3. StringUtils.equals(null, "abc") = false
  4. StringUtils.equals("abc", null) = false
  5. StringUtils.equals("abc", "abc") = true
  6. StringUtils.equals("abc", "ABC") = false
  7. ---------------------------------------
  8. StringUtils.equalsIgnoreCase(null, null) = true
  9. StringUtils.equalsIgnoreCase(null, "abc") = false
  10. StringUtils.equalsIgnoreCase("abc", null) = false
  11. StringUtils.equalsIgnoreCase("abc", "abc") = true
  12. StringUtils.equalsIgnoreCase("abc", "ABC") = true

2,包含其中

  1. StringUtils.contains(null, *) = false
  2. StringUtils.contains(*, null) = false
  3. StringUtils.contains("", "") = true
  4. StringUtils.contains("abc", "") = true
  5. StringUtils.contains("abc", "a") = true
  6. StringUtils.contains("abc", "z") = false

3,匹配头尾

  1. StringUtils.startsWith(null, null) = true
  2. StringUtils.startsWith(null, "abc") = false
  3. StringUtils.startsWith("abcdef", null) = false
  4. StringUtils.startsWith("abcdef", "abc") = true
  5. StringUtils.startsWith("ABCDEF", "abc") = false
  6. StringUtils.endsWith(null, null) = true
  7. StringUtils.endsWith(null, "def") = false
  8. StringUtils.endsWith("abcdef", null) = false
  9. StringUtils.endsWith("abcdef", "def") = true
  10. StringUtils.endsWith("ABCDEF", "def") = false
  11. StringUtils.endsWith("ABCDEF", "cde") = false
  12. StringUtils.endsWith("ABCDEF", "") = true

4,命中位置

  1. StringUtils.indexOf(null, *) = -1
  2. StringUtils.indexOf("", *) = -1
  3. StringUtils.indexOf("aabaabaa", 'a') = 0
  4. StringUtils.indexOf("aabaabaa", 'b') = 2
  5. ---------------------------------------
  6. StringUtils.lastIndexOf(null, *) = -1
  7. StringUtils.lastIndexOf("", *) = -1
  8. StringUtils.lastIndexOf("aabaabaa", 'a') = 7
  9. StringUtils.lastIndexOf("aabaabaa", 'b') = 5

5,命中数量

  1. StringUtils.countMatches(null, *) = 0
  2. StringUtils.countMatches("", *) = 0
  3. StringUtils.countMatches("abba", null) = 0
  4. StringUtils.countMatches("abba", "") = 0
  5. StringUtils.countMatches("abba", "a") = 2
  6. StringUtils.countMatches("abba", "ab") = 1
  7. StringUtils.countMatches("abba", "xxx") = 0

6,差异之处

  1. StringUtils.difference(null, null) = null
  2. StringUtils.difference("", "") = ""
  3. StringUtils.difference("", "abc") = "abc"
  4. StringUtils.difference("abc", "") = ""
  5. StringUtils.difference("abc", "abc") = ""
  6. StringUtils.difference("abc", "ab") = ""
  7. StringUtils.difference("ab", "abxyz") = "xyz"
  8. StringUtils.difference("abcde", "abxyz") = "xyz"
  9. StringUtils.difference("abcde", "xyz") = "xyz"

一,去除

1,去空格

  1. StringUtils.trim(null) = null
  2. StringUtils.trim("") = ""
  3. StringUtils.trim(" ") = ""
  4. StringUtils.trim("abc") = "abc"
  5. StringUtils.trim(" abc ") = "abc"
  6. ---------------------------------------
  7. StringUtils.trimToEmpty(null) = ""
  8. StringUtils.trimToEmpty("") = ""
  9. StringUtils.trimToEmpty(" ") = ""
  10. StringUtils.trimToEmpty("abc") = "abc"
  11. StringUtils.trimToEmpty(" abc ") = "abc"

2,去换行符

  1. StringUtils.chop(null) = null
  2. StringUtils.chop("") = ""
  3. StringUtils.chop("abc \r") = "abc "
  4. StringUtils.chop("abc\n") = "abc"
  5. StringUtils.chop("abc\r\n") = "abc"
  6. StringUtils.chop("abc") = "ab"
  7. StringUtils.chop("abc\nabc") = "abc\nab"
  8. StringUtils.chop("a") = ""
  9. StringUtils.chop("\r") = ""
  10. StringUtils.chop("\n") = ""
  11. StringUtils.chop("\r\n") = ""

3,去特定字符

  1. StringUtils.removeStart(null, *) = null
  2. StringUtils.removeStart("", *) = ""
  3. StringUtils.removeStart(*, null) = *
  4. StringUtils.removeStart("www.domain.com", "www.") = "domain.com"
  5. StringUtils.removeStart("domain.com", "www.") = "domain.com"
  6. StringUtils.removeStart("www.domain.com", "domain") = "www.domain.com"
  7. StringUtils.removeStart("abc", "") = "abc"
  8. ---------------------------------------
  9. StringUtils.removeEnd(null, *) = null
  10. StringUtils.removeEnd("", *) = ""
  11. StringUtils.removeEnd(*, null) = *
  12. StringUtils.removeEnd("www.domain.com", ".com.") = "www.domain.com"
  13. StringUtils.removeEnd("www.domain.com", ".com") = "www.domain"
  14. StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com"
  15. StringUtils.removeEnd("abc", "") = "abc"
  16. ---------------------------------------
  17. StringUtils.remove(null, *) = null
  18. StringUtils.remove("", *) = ""
  19. StringUtils.remove("queued", 'u') = "qeed"
  20. StringUtils.remove("queued", 'z') = "queued"

三,转化

1,截取前N个

  1. StringUtils.truncate(null, 0) = null
  2. StringUtils.truncate(null, 2) = null
  3. StringUtils.truncate("", 4) = ""
  4. StringUtils.truncate("abcdefg", 4) = "abcd"
  5. StringUtils.truncate("abcdefg", 8) = "abcdefg"

2,替换

  1. StringUtils.replaceOnce(null, *, *) = null
  2. StringUtils.replaceOnce("", *, *) = ""
  3. StringUtils.replaceOnce("any", null, *) = "any"
  4. StringUtils.replaceOnce("any", *, null) = "any"
  5. StringUtils.replaceOnce("any", "", *) = "any"
  6. StringUtils.replaceOnce("aba", "a", null) = "aba"
  7. StringUtils.replaceOnce("aba", "a", "") = "ba"
  8. StringUtils.replaceOnce("aba", "a", "z") = "zba"
  9. ---------------------------------------
  10. StringUtils.replace(null, *, *) = null
  11. StringUtils.replace("", *, *) = ""
  12. StringUtils.replace("any", null, *) = "any"
  13. StringUtils.replace("any", *, null) = "any"
  14. StringUtils.replace("any", "", *) = "any"
  15. StringUtils.replace("aba", "a", null) = "aba"
  16. StringUtils.replace("aba", "a", "") = "b"
  17. StringUtils.replace("aba", "a", "z") = "zbz"

3,分割

  1. StringUtils.split(null, *) = null
  2. StringUtils.split("", *) = []
  3. StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
  4. StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
  5. StringUtils.split("a:b:c", '.') = ["a:b:c"]
  6. StringUtils.split("a b c", ' ') = ["a", "b", "c"]

4,合并

  1. StringUtils.join(null) = null
  2. StringUtils.join([]) = ""
  3. StringUtils.join([null]) = ""
  4. StringUtils.join(["a", "b", "c"]) = "abc"
  5. StringUtils.join([null, "", "a"]) = "a"
  6. ---------------------------------------
  7. StringUtils.join(null, *) = null
  8. StringUtils.join([], *) = ""
  9. StringUtils.join([null], *) = ""
  10. StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
  11. StringUtils.join(["a", "b", "c"], null) = "abc"
  12. StringUtils.join([null, "", "a"], ';') = ";;a"

5,大小写

  1. StringUtils.upperCase(null) = null
  2. StringUtils.upperCase("") = ""
  3. StringUtils.upperCase("aBc") = "ABC"
  4. ---------------------------------------
  5. StringUtils.lowerCase(null) = null
  6. StringUtils.lowerCase("") = ""
  7. StringUtils.lowerCase("aBc") = "abc"

四,比较

  1. str1.compareTo(str2)
  2. StringUtils.compare(null, null) = 0
  3. StringUtils.compare(null , "a") < 0
  4. StringUtils.compare("a", null) > 0
  5. StringUtils.compare("abc", "abc") = 0
  6. StringUtils.compare("a", "b") < 0
  7. StringUtils.compare("b", "a") > 0
  8. StringUtils.compare("a", "B") > 0
  9. StringUtils.compare("ab", "abc") < 0