CommonLang3中的StringUtils最全解析
isEmpty
public static boolean isEmpty(CharSequence cs)常用函數之一,判斷字符串是否為""或者null
StringUtils.isEmpty(null) = trueStringUtils.isEmpty("") = trueStringUtils.isEmpty(" ") = falseStringUtils.isEmpty("bob") = falseStringUtils.isEmpty(" bob ") = falseisNotEmpty
public static boolean isNotEmpty(CharSequence cs)最常用函數之一,跟上面方法相對
StringUtils.isNotEmpty(null) = falseStringUtils.isNotEmpty("") = falseStringUtils.isNotEmpty(" ") = trueStringUtils.isNotEmpty("bob") = trueStringUtils.isNotEmpty(" bob ") = trueisAnyEmpty
public static boolean isAnyEmpty(CharSequence... css)任意一個參數為空的話,返回true, 如果這些參數都不為空的話返回false。
在寫一些判斷條件的時候,這個方法還是很實用的。
StringUtils.isAnyEmpty(null) = trueStringUtils.isAnyEmpty(null, "foo") = trueStringUtils.isAnyEmpty("", "bar") = trueStringUtils.isAnyEmpty("bob", "") = trueStringUtils.isAnyEmpty(" bob ", null) = trueStringUtils.isAnyEmpty(" ", "bar") = falseStringUtils.isAnyEmpty("foo", "bar") = falseisNoneEmpty
public static boolean isNoneEmpty(CharSequence... css)任意一個參數是空,返回false 所有參數都不為空,返回true
注意這些方法的用法
StringUtils.isNoneEmpty(null) = falseStringUtils.isNoneEmpty(null, "foo") = falseStringUtils.isNoneEmpty("", "bar") = falseStringUtils.isNoneEmpty("bob", "") = falseStringUtils.isNoneEmpty(" bob ", null) = falseStringUtils.isNoneEmpty(" ", "bar") = trueStringUtils.isNoneEmpty("foo", "bar") = trueisBlank
public static boolean isBlank(CharSequence cs)判斷字符對象是不是空字符串,注意與isEmpty的區別
StringUtils.isBlank(null) = trueStringUtils.isBlank("") = trueStringUtils.isBlank(" ") = trueStringUtils.isBlank("bob") = falseStringUtils.isBlank(" bob ") = falseisNotBlank
public static boolean isNotBlank(CharSequence cs)原理同上
StringUtils.isNotBlank(null) = falseStringUtils.isNotBlank("") = falseStringUtils.isNotBlank(" ") = falseStringUtils.isNotBlank("bob") = trueStringUtils.isNotBlank(" bob ") = trueisAnyBlank
public static boolean isAnyBlank(CharSequence... css) 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") = falseisNoneBlank
public static boolean isNoneBlank(CharSequence... css) 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") = truetrim
public static String trim(String str)移除字符串兩端的空字符串,制表符char <= 32如:\n \t 如果為空的話,返回空
如果為""
StringUtils.trim(null) = nullStringUtils.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, *) = nullStringUtils.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) = trueStringUtils.equals(null, "abc") = falseStringUtils.equals("abc", null) = falseStringUtils.equals("abc", "abc") = trueStringUtils.equals("abc", "ABC") = falseequalsIgnoreCse
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") = trueindexOf
public static int indexOf(CharSequence seq, int searchChar)indexOf這個方法不必多說,這個方法主要處理掉了空字符串的問題,不會報空指針,有一定用處
StringUtils.indexOf(null, *) = -1StringUtils.indexOf("", *) = -1StringUtils.indexOf("aabaabaa", 'a') = 0StringUtils.indexOf("aabaabaa", 'b') = 2ordinalIndexOf
public static int ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal)字符串在另外一個字符串里,出現第Ordinal次的位置
StringUtils.ordinalIndexOf(null, *, *) = -1StringUtils.ordinalIndexOf(*, null, *) = -1StringUtils.ordinalIndexOf("", "", *) = 0StringUtils.ordinalIndexOf("aabaabaa", "a", 1) = 0StringUtils.ordinalIndexOf("aabaabaa", "a", 2) = 1StringUtils.ordinalIndexOf("aabaabaa", "b", 1) = 2StringUtils.ordinalIndexOf("aabaabaa", "b", 2) = 5StringUtils.ordinalIndexOf("aabaabaa", "ab", 1) = 1StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) = 4StringUtils.ordinalIndexOf("aabaabaa", "", 1) = 0StringUtils.ordinalIndexOf("aabaabaa", "", 2) = 0lastIndexOf
public static int lastIndexOf(CharSequence seq, int searchChar)字符串最后一次出現的位置
StringUtils.lastIndexOf(null, *) = -1StringUtils.lastIndexOf("", *) = -1StringUtils.lastIndexOf("aabaabaa", 'a') = 7StringUtils.lastIndexOf("aabaabaa", 'b') = 5lastOrdinalIndexOf
public static int lastOrdinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal)字符串searchStr在str里面出現倒數第ordinal出現的位置
StringUtils.lastOrdinalIndexOf(null, *, *) = -1StringUtils.lastOrdinalIndexOf(*, null, *) = -1StringUtils.lastOrdinalIndexOf("", "", *) = 0StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 1) = 7StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 2) = 6StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 1) = 5StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 2) = 2StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 1) = 4StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2) = 1StringUtils.lastOrdinalIndexOf("aabaabaa", "", 1) = 8StringUtils.lastOrdinalIndexOf("aabaabaa", "", 2) = 8contains
public static boolean contains(CharSequence seq, int searchChar)字符串seq是否包含searchChar
StringUtils.contains(null, *) = falseStringUtils.contains("", *) = falseStringUtils.contains("abc", 'a') = trueStringUtils.contains("abc", 'z') = falsecontainsAny
public static boolean containsAny(CharSequence cs, char... searchChars)包含后面數組中的任意對象,返回true
StringUtils.containsAny(null, *) = falseStringUtils.containsAny("", *) = falseStringUtils.containsAny(*, null) = falseStringUtils.containsAny(*, []) = falseStringUtils.containsAny("zzabyycdxx",['z','a']) = trueStringUtils.containsAny("zzabyycdxx",['b','y']) = trueStringUtils.containsAny("aba", ['z']) = falsesubstring
public static String substring(String str, int start)字符串截取
StringUtils.substring(null, *) = nullStringUtils.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, *) = nullStringUtils.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) = nullStringUtils.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, *) = nullStringUtils.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) = nullStringUtils.deleteWhitespace("") = ""StringUtils.deleteWhitespace("abc") = "abc"StringUtils.deleteWhitespace(" ab c ") = "abc"removeStart
public static String removeStart(String str, String remove)刪除以特定字符串開頭的字符串,如果沒有的話,就不刪除。
StringUtils.removeStart(null, *) = nullStringUtils.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, *, *) = nullStringUtils.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, *, *) = nullStringUtils.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, *) = nullStringUtils.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) = nullStringUtils.capitalize("") = ""StringUtils.capitalize("cat") = "Cat"StringUtils.capitalize("cAt") = "CAt"swapCase
public static String swapCase(String str)反向大小寫
StringUtils.swapCase(null) = nullStringUtils.swapCase("") = ""StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"isAlpha
public static boolean isAlpha(CharSequence cs)判斷字符串是否由字母組成
StringUtils.isAlpha(null) = falseStringUtils.isAlpha("") = falseStringUtils.isAlpha(" ") = falseStringUtils.isAlpha("abc") = trueStringUtils.isAlpha("ab2c") = falseStringUtils.isAlpha("ab-c") = falsedefaultString
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) = nullStringUtils.reverse("") = ""StringUtils.reverse("bat") = "tab"abbreviate
public static String abbreviate(String str, int maxWidth)縮略字符串, 省略號要占三位。maxWith小于3位會報錯。
StringUtils.abbreviate(null, *) = nullStringUtils.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, *, *) = nullStringUtils.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) = IllegalArgumentExceptionStringUtils.abbreviate("abcdefghij", 5, 6) = IllegalArgumentExceptionwarp
public static String wrap(String str, char wrapWith)包裝,用后面的字符串對前面的字符串進行包裝
StringUtils.wrap(null, *) = nullStringUtils.wrap("", *) = ""StringUtils.wrap("ab", '\0') = "ab"StringUtils.wrap("ab", 'x') = "xabx"StringUtils.wrap("ab", '\'') = "'ab'"StringUtils.wrap("\"ab\"", '\"') = "\"\"ab\"\""轉載于:https://my.oschina.net/tensai/blog/3012993
總結
以上是生活随笔為你收集整理的CommonLang3中的StringUtils最全解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】别人整理的DP大全
- 下一篇: JBoss 系列五十:使用Apache