CommonLang3中的StringUtils最全解析

isEmpty

public static boolean isEmpty(CharSequence cs)

經常使用函數之一,判斷字符串是否爲""或者nullcss

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

isNotEmpty

public static boolean isNotEmpty(CharSequence cs)

最經常使用函數之一,跟上面方法相對java

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

isAnyEmpty

public static boolean isAnyEmpty(CharSequence... css)

任意一個參數爲空的話,返回true, 若是這些參數都不爲空的話返回false。api

在寫一些判斷條件的時候,這個方法仍是很實用的。數組

StringUtils.isAnyEmpty(null)             = true
 StringUtils.isAnyEmpty(null, "foo")      = true
 StringUtils.isAnyEmpty("", "bar")        = true
 StringUtils.isAnyEmpty("bob", "")        = true
 StringUtils.isAnyEmpty("  bob  ", null)  = true
 StringUtils.isAnyEmpty(" ", "bar")       = false
 StringUtils.isAnyEmpty("foo", "bar")     = false

isNoneEmpty

public static boolean isNoneEmpty(CharSequence... css)

任意一個參數是空,返回false 全部參數都不爲空,返回truedom

注意這些方法的用法函數

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

isBlank

public static boolean isBlank(CharSequence cs)

判斷字符對象是否是空字符串,注意與isEmpty的區別spa

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

isNotBlank

public static boolean isNotBlank(CharSequence cs)

原理同上指針

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

isAnyBlank

public static boolean isAnyBlank(CharSequence... css)
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("foo", "bar")     = false

isNoneBlank

public static boolean isNoneBlank(CharSequence... css)
StringUtils.isNoneBlank(null)             = false
 StringUtils.isNoneBlank(null, "foo")      = false
 StringUtils.isNoneBlank(null, null)       = false
 StringUtils.isNoneBlank("", "bar")        = false
 StringUtils.isNoneBlank("bob", "")        = false
 StringUtils.isNoneBlank("  bob  ", null)  = false
 StringUtils.isNoneBlank(" ", "bar")       = false
 StringUtils.isNoneBlank("foo", "bar")     = true

trim

public static String trim(String str)

移除字符串兩端的空字符串,製表符char <= 32如:\n \t 若是爲空的話,返回空code

若是爲""對象

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

變體有

public static String trimToNull(String str)
public static String trimToEmpty(String str)

不經常使用,跟trim()方法相似

strip

public static String strip(String str)
public static String strip(String str, String stripChars)

str:被處理的字符串,可爲空 stripChars: 刪除的字符串,

StringUtils.strip(null, *)          = null
 StringUtils.strip("", *)            = ""
 StringUtils.strip("abc", null)      = "abc"
 StringUtils.strip("  abc", null)    = "abc"
 StringUtils.strip("abc  ", null)    = "abc"
 StringUtils.strip(" abc ", null)    = "abc"
 StringUtils.strip("  abcyx", "xyz") = "  abc"

equals

public static boolean equals(CharSequence cs1, CharSequence cs2)

字符串比對方法,是比較實用的方法之一,兩個比較的字符串都能爲空,不會報空指針異常。

StringUtils.equals(null, null)   = true
 StringUtils.equals(null, "abc")  = false
 StringUtils.equals("abc", null)  = false
 StringUtils.equals("abc", "abc") = true
 StringUtils.equals("abc", "ABC") = false

equalsIgnoreCse

public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2)

上面方法的變體 字符串比較(忽略大小寫),在驗證碼……等字符串比較,真是很實用。

StringUtils.equalsIgnoreCase(null, null)   = true
StringUtils.equalsIgnoreCase(null, "abc")  = false
StringUtils.equalsIgnoreCase("abc", null)  = false
StringUtils.equalsIgnoreCase("abc", "abc") = true
StringUtils.equalsIgnoreCase("abc", "ABC") = true

indexOf

public static int indexOf(CharSequence seq, int searchChar)

indexOf這個方法沒必要多說,這個方法主要處理掉了空字符串的問題,不會報空指針,有必定用處

StringUtils.indexOf(null, *)         = -1
 StringUtils.indexOf("", *)           = -1
 StringUtils.indexOf("aabaabaa", 'a') = 0
 StringUtils.indexOf("aabaabaa", 'b') = 2

ordinalIndexOf

public static int ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal)

字符串在另一個字符串裏,出現第Ordinal次的位置

StringUtils.ordinalIndexOf(null, *, *)          = -1
 StringUtils.ordinalIndexOf(*, null, *)          = -1
 StringUtils.ordinalIndexOf("", "", *)           = 0
 StringUtils.ordinalIndexOf("aabaabaa", "a", 1)  = 0
 StringUtils.ordinalIndexOf("aabaabaa", "a", 2)  = 1
 StringUtils.ordinalIndexOf("aabaabaa", "b", 1)  = 2
 StringUtils.ordinalIndexOf("aabaabaa", "b", 2)  = 5
 StringUtils.ordinalIndexOf("aabaabaa", "ab", 1) = 1
 StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) = 4
 StringUtils.ordinalIndexOf("aabaabaa", "", 1)   = 0
 StringUtils.ordinalIndexOf("aabaabaa", "", 2)   = 0

lastIndexOf

public static int lastIndexOf(CharSequence seq, int searchChar)

字符串最後一次出現的位置

StringUtils.lastIndexOf(null, *)         = -1
 StringUtils.lastIndexOf("", *)           = -1
 StringUtils.lastIndexOf("aabaabaa", 'a') = 7
 StringUtils.lastIndexOf("aabaabaa", 'b') = 5

lastOrdinalIndexOf

public static int lastOrdinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal)

字符串searchStr在str裏面出現倒數第ordinal出現的位置

StringUtils.lastOrdinalIndexOf(null, *, *)          = -1
 StringUtils.lastOrdinalIndexOf(*, null, *)          = -1
 StringUtils.lastOrdinalIndexOf("", "", *)           = 0
 StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 1)  = 7
 StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 2)  = 6
 StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 1)  = 5
 StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 2)  = 2
 StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 1) = 4
 StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2) = 1
 StringUtils.lastOrdinalIndexOf("aabaabaa", "", 1)   = 8
 StringUtils.lastOrdinalIndexOf("aabaabaa", "", 2)   = 8

contains

public static boolean contains(CharSequence seq, int searchChar)

字符串seq是否包含searchChar

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

containsAny

public static boolean containsAny(CharSequence cs, char... searchChars)

包含後面數組中的任意對象,返回true

StringUtils.containsAny(null, *)                = false
 StringUtils.containsAny("", *)                  = false
 StringUtils.containsAny(*, null)                = false
 StringUtils.containsAny(*, [])                  = false
 StringUtils.containsAny("zzabyycdxx",['z','a']) = true
 StringUtils.containsAny("zzabyycdxx",['b','y']) = true
 StringUtils.containsAny("aba", ['z'])           = false

substring

public static String substring(String str, int start)

字符串截取

StringUtils.substring(null, *)   = null
 StringUtils.substring("", *)     = ""
 StringUtils.substring("abc", 0)  = "abc"
 StringUtils.substring("abc", 2)  = "c"
 StringUtils.substring("abc", 4)  = ""
 StringUtils.substring("abc", -2) = "bc"
 StringUtils.substring("abc", -4) = "abc"

left right mid

public static String left(String str, int len)
public static String right(String str, int len)
public static String mid(String str, int pos, int len)

這三個方法相似都是截取字符串

split

public static String[] split(String str, String separatorChars)

字符串分割

StringUtils.split(null, *)         = null
 StringUtils.split("", *)           = []
 StringUtils.split("abc def", null) = ["abc", "def"]
 StringUtils.split("abc def", " ")  = ["abc", "def"]
 StringUtils.split("abc  def", " ") = ["abc", "def"]
 StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]

join

public static <T> String join(T... elements)

字符串鏈接

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

join

public static String join(Object[] array, char separator)

特定字符串鏈接數組,不少狀況下仍是蠻實用,不用本身取拼字符串

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

deleteWhitespace

public static String deleteWhitespace(String str)

刪除空格

StringUtils.deleteWhitespace(null)         = null
 StringUtils.deleteWhitespace("")           = ""
 StringUtils.deleteWhitespace("abc")        = "abc"
 StringUtils.deleteWhitespace("   ab  c  ") = "abc"

removeStart

public static String removeStart(String str, String remove)

刪除以特定字符串開頭的字符串,若是沒有的話,就不刪除。

StringUtils.removeStart(null, *)      = null
 StringUtils.removeStart("", *)        = ""
 StringUtils.removeStart(*, null)      = *
 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"

rightPad

public static String rightPad(String str, int size, char padChar)

生成訂單號,的時候仍是很實用的。右邊自動補齊。

StringUtils.rightPad(null, *, *)     = null
 StringUtils.rightPad("", 3, 'z')     = "zzz"
 StringUtils.rightPad("bat", 3, 'z')  = "bat"
 StringUtils.rightPad("bat", 5, 'z')  = "batzz"
 StringUtils.rightPad("bat", 1, 'z')  = "bat"
 StringUtils.rightPad("bat", -1, 'z') = "bat"

leftPad

public static String leftPad(String str, int size, char padChar)

左邊自動補齊

StringUtils.leftPad(null, *, *)     = null
 StringUtils.leftPad("", 3, 'z')     = "zzz"
 StringUtils.leftPad("bat", 3, 'z')  = "bat"
 StringUtils.leftPad("bat", 5, 'z')  = "zzbat"
 StringUtils.leftPad("bat", 1, 'z')  = "bat"
 StringUtils.leftPad("bat", -1, 'z') = "bat"

center

public static String center(String str, int size)

將字符在某特定長度下,句子

StringUtils.center(null, *)   = null
 StringUtils.center("", 4)     = "    "
 StringUtils.center("ab", -1)  = "ab"
 StringUtils.center("ab", 4)   = " ab "
 StringUtils.center("abcd", 2) = "abcd"
 StringUtils.center("a", 4)    = " a  "

capitalize

public static String capitalize(String str)

首字母大寫

StringUtils.capitalize(null)  = null
 StringUtils.capitalize("")    = ""
 StringUtils.capitalize("cat") = "Cat"
 StringUtils.capitalize("cAt") = "CAt"

swapCase

public static String swapCase(String str)

反向大小寫

StringUtils.swapCase(null)                 = null
 StringUtils.swapCase("")                   = ""
 StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"

isAlpha

public static boolean isAlpha(CharSequence cs)

判斷字符串是否由字母組成

StringUtils.isAlpha(null)   = false
 StringUtils.isAlpha("")     = false
 StringUtils.isAlpha("  ")   = false
 StringUtils.isAlpha("abc")  = true
 StringUtils.isAlpha("ab2c") = false
 StringUtils.isAlpha("ab-c") = false

defaultString

public static String defaultString(String str, String defaultStr)

默認字符串,至關於三目運算,前面弱爲空,則返回後面一個參數

StringUtils.defaultString(null, "NULL")  = "NULL"
 StringUtils.defaultString("", "NULL")    = ""
 StringUtils.defaultString("bat", "NULL") = "bat"

reverse

public static String reverse(String str)

字符串翻轉

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

abbreviate

public static String abbreviate(String str, int maxWidth)

縮略字符串, 省略號要佔三位。maxWith小於3位會報錯。

StringUtils.abbreviate(null, *)      = null
 StringUtils.abbreviate("", 4)        = ""
 StringUtils.abbreviate("abcdefg", 6) = "abc..."
 StringUtils.abbreviate("abcdefg", 7) = "abcdefg"
 StringUtils.abbreviate("abcdefg", 8) = "abcdefg"
 StringUtils.abbreviate("abcdefg", 4) = "a..."
 StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException
public static String abbreviate(String str, int offset, int maxWidth)

縮略字符串的一些高級用法

StringUtils.abbreviate(null, *, *)                = null
 StringUtils.abbreviate("", 0, 4)                  = ""
 StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..."
 StringUtils.abbreviate("abcdefghijklmno", 0, 10)  = "abcdefg..."
 StringUtils.abbreviate("abcdefghijklmno", 1, 10)  = "abcdefg..."
 StringUtils.abbreviate("abcdefghijklmno", 4, 10)  = "abcdefg..."
 StringUtils.abbreviate("abcdefghijklmno", 5, 10)  = "...fghi..."
 StringUtils.abbreviate("abcdefghijklmno", 6, 10)  = "...ghij..."
 StringUtils.abbreviate("abcdefghijklmno", 8, 10)  = "...ijklmno"
 StringUtils.abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno"
 StringUtils.abbreviate("abcdefghijklmno", 12, 10) = "...ijklmno"
 StringUtils.abbreviate("abcdefghij", 0, 3)        = IllegalArgumentException
 StringUtils.abbreviate("abcdefghij", 5, 6)        = IllegalArgumentException

warp

public static String wrap(String str, char wrapWith)

包裝,用後面的字符串對前面的字符串進行包裝

StringUtils.wrap(null, *)        = null
 StringUtils.wrap("", *)          = ""
 StringUtils.wrap("ab", '\0')     = "ab"
 StringUtils.wrap("ab", 'x')      = "xabx"
 StringUtils.wrap("ab", '\'')     = "'ab'"
 StringUtils.wrap("\"ab\"", '\"') = "\"\"ab\"\""
相關文章
相關標籤/搜索