六、ObjectUtils
Object工具類
allNotNull(Object... values) 檢查所有元素是否為空,返回一個boolean
如果有一個元素為空返回false,所有元素不為空或元素為empty返回true
ObjectUtils.allNotNull(*) = true ObjectUtils.allNotNull(*, *) = true ObjectUtils.allNotNull(null) = false ObjectUtils.allNotNull(null, null) = false ObjectUtils.allNotNull(null, *) = false ObjectUtils.allNotNull(*, null) = false ObjectUtils.allNotNull(*, *, null, *) = false
anyNotNull(Object... values) 檢查元素是否為空,返回一個boolean
如果有一個元素不為空返回true
ObjectUtils.anyNotNull(*) = true ObjectUtils.anyNotNull(*, null) = true ObjectUtils.anyNotNull(null, *) = true ObjectUtils.anyNotNull(null, null, *, *) = true ObjectUtils.anyNotNull(null) = false ObjectUtils.anyNotNull(null, null) = false
clone(T obj) 拷貝一個對象並返回
compare(T c1, T c2) 比較兩個對象,返回一個int值
defaultIfNull(T object, T defaultValue) 如果對象為空返回一個默認值
firstNonNull(T... values) 返回數組中第一個不為空的值
notEqual(Object object1, Object object2) 判斷兩個對象不相等,返回一個boolean
七、RandomUtils
隨機工具類
nextBoolean() 返回一個隨機boolean值
nextBytes(int count) 返回一個指定大小的隨機byte數組
nextDouble() 返回一個隨機double值
nextDouble(double startInclusive, double endInclusive) 返回一個指定范圍的隨機double值
nextFloat() 返回一個隨機float值
nextFloat(float startInclusive, float endInclusive) 返回一個指定范圍的隨機float值
nextInt() 返回一個隨機int值
nextInt(int startInclusive, int endExclusive) 返回一個指定范圍的隨機int值
nextLong() 返回一個隨機long值
nextLong(long startInclusive, long endExclusive) 返回一個指定范圍的隨機long值
八、SystemUtils
操作系統工具類
FILE_ENCODING 返回系統編碼
IS_JAVA_1_1、...、IS_JAVA_1_8、IS_JAVA_10、IS_JAVA_9 判斷java版本,返回一個boolean
IS_OS_LINUX 判斷系統是否是linux,返回一個boolean
IS_OS_MAC 判斷系統是否是mac,返回一個boolean
IS_OS_WINDOWS、IS_OS_WINDOWS_10、 IS_OS_WINDOWS_2000、IS_OS_WINDOWS_2003、IS_OS_WINDOWS_2008、IS_OS_WINDOWS_7、 IS_OS_WINDOWS_8、 IS_OS_WINDOWS_95、 IS_OS_WINDOWS_98、 IS_OS_WINDOWS_XP 判斷系統是否是windows,返回一個boolean
JAVA_CLASS_PATH 返回系統CLASS_PATH值
JAVA_CLASS_VERSION 返回系統java版本
JAVA_HOME 返回系統java home
JAVA_RUNTIME_VERSION 返回java運行版本
JAVA_VERSION 返回java版本
OS_NAME 返回系統名
OS_VERSION 返回系統版本
USER_COUNTRY 返回用戶國家編號
USER_DIR 返回項目文件夾
USER_HOME 返回系統用戶主文件夾
USER_LANGUAGE 返回系統用戶語言
USER_NAME 返回系統用戶名
九、StringUtils
字符串工具類
abbreviate(String str, int maxWidth) 返回一個指定長度加省略號的字符串,maxWidth必須大於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
abbreviate(String str, int offset, int maxWidth) 返回一個指定長度加省略號的字符串,maxWidth必須大於3
abbreviate(String str, String abbrevMarker, int maxWidth) 返回一個自定義省略號的指定長度字符串,maxWidth必須大於3
StringUtils.abbreviate(null, "...", *) = null StringUtils.abbreviate("abcdefg", null, *) = "abcdefg" StringUtils.abbreviate("", "...", 4) = "" StringUtils.abbreviate("abcdefg", ".", 5) = "abcd." StringUtils.abbreviate("abcdefg", ".", 7) = "abcdefg" StringUtils.abbreviate("abcdefg", ".", 8) = "abcdefg" StringUtils.abbreviate("abcdefg", "..", 4) = "ab.." StringUtils.abbreviate("abcdefg", "..", 3) = "a.." StringUtils.abbreviate("abcdefg", "..", 2) = IllegalArgumentException StringUtils.abbreviate("abcdefg", "...", 3) = IllegalArgumentException
abbreviateMiddle(String str, String abbrevMarker, int maxWidth) 將字符串縮短到指定長度(length),字符串的中間部分用替換字符串(middle)顯示
StringUtils.abbreviateMiddle("abc", null, 0) = "abc" StringUtils.abbreviateMiddle("abc", ".", 0) = "abc" StringUtils.abbreviateMiddle("abc", ".", 3) = "abc" StringUtils.abbreviateMiddle("abcdef", ".", 4) = "ab.f"
appendIfMissing(String str, CharSequence suffix, CharSequence... suffixes) 如果str不是以任何suffixes結尾,將字符串suffix拼接到字符串str后面
StringUtils.appendIfMissing(null, null) = null StringUtils.appendIfMissing("abc", null) = "abc" StringUtils.appendIfMissing("", "xyz") = "xyz" StringUtils.appendIfMissing("abc", "xyz") = "abcxyz" StringUtils.appendIfMissing("abcxyz", "xyz") = "abcxyz" StringUtils.appendIfMissing("abcXYZ", "xyz") = "abcXYZxyz"
appendIfMissingIgnoreCase(String str, CharSequence suffix, CharSequence... suffixes) 同上 不區分大小寫
capitalize(String str) 將字符串第一個字符大寫並返回
center(String str, int size) 用空格字符填充使字符串str位於長度為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 "
center(String str, int size, char padChar) 用指定字符填充使字符串str位於長度為size的大字符串中間
chomp(String str) 刪除字符串末尾的一個換行符,返回一個新的字符串(換行符"n", "r", or "rn")
StringUtils.chomp(null) = null StringUtils.chomp("") = "" StringUtils.chomp("abc \r") = "abc " StringUtils.chomp("abc\n") = "abc" StringUtils.chomp("abc\r\n") = "abc" StringUtils.chomp("abc\r\n\r\n") = "abc\r\n" StringUtils.chomp("abc\n\r") = "abc\n" StringUtils.chomp("abc\n\rabc") = "abc\n\rabc" StringUtils.chomp("\r") = "" StringUtils.chomp("\n") = "" StringUtils.chomp("\r\n") = ""
chop(String str) 刪除字符串末尾的一個字符,返回一個新的字符串
StringUtils.chop(null) = null StringUtils.chop("") = "" StringUtils.chop("abc \r") = "abc " StringUtils.chop("abc\n") = "abc" StringUtils.chop("abc\r\n") = "abc" StringUtils.chop("abc") = "ab" StringUtils.chop("abc\nabc") = "abc\nab" StringUtils.chop("a") = "" StringUtils.chop("\r") = "" StringUtils.chop("\n") = "" StringUtils.chop("\r\n") = ""
compare(String str1, String str2) 比較兩個字符串,返回一個int值
//str1等於str2(或都為空)返回0 //str1小於str2返回小於0 //str1大於str2返回大於0 StringUtils.compare(null, null) = 0 StringUtils.compare(null , "a") < 0 StringUtils.compare("a", null) > 0 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
contains(CharSequence seq, CharSequence searchSeq) 檢查字符串中是否包含指定字符,返回boolean
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
containsAny(CharSequence cs, CharSequence... searchCharSequences) 檢查字符串中是否包含任一字符,返回boolean
StringUtils.containsAny(null, *) = false StringUtils.containsAny("", *) = false StringUtils.containsAny(*, null) = false StringUtils.containsAny(*, []) = false StringUtils.containsAny("abcd", "ab", null) = true StringUtils.containsAny("abcd", "ab", "cd") = true StringUtils.containsAny("abc", "d", "abc") = true
containsNone(CharSequence cs, String invalidChars) 檢查字符串不包含指定字符,返回boolean
StringUtils.containsNone(null, *) = true StringUtils.containsNone(*, null) = true StringUtils.containsNone("", *) = true StringUtils.containsNone("ab", "") = true StringUtils.containsNone("abab", "xyz") = true StringUtils.containsNone("ab1", "xyz") = true StringUtils.containsNone("abz", "xyz") = false
containsOnly(CharSequence cs, String validChars) 檢查字符串只包含特定的字符,返回boolean
StringUtils.containsOnly(null, *) = false StringUtils.containsOnly(*, null) = false StringUtils.containsOnly("", *) = true StringUtils.containsOnly("ab", "") = false StringUtils.containsOnly("abab", "abc") = true StringUtils.containsOnly("ab1", "abc") = false StringUtils.containsOnly("abz", "abc") = false
containsWhitespace(CharSequence seq) 檢查字符串中是否包含空格字符,返回boolean
countMatches(CharSequence str, CharSequence sub) 檢查字符串中出現指定字符的次數,返回一個int值
StringUtils.countMatches(null, *) = 0 StringUtils.countMatches("", *) = 0 StringUtils.countMatches("abba", null) = 0 StringUtils.countMatches("abba", "") = 0 StringUtils.countMatches("abba", "a") = 2 StringUtils.countMatches("abba", "ab") = 1 StringUtils.countMatches("abba", "xxx") = 0
defaultIfBlank(T str, T defaultStr) 如果字符串為null、空(""),或全是空格,將返回指定字符串,否則返回原值
StringUtils.defaultIfBlank(null, "NULL") = "NULL" StringUtils.defaultIfBlank("", "NULL") = "NULL" StringUtils.defaultIfBlank(" ", "NULL") = "NULL" StringUtils.defaultIfBlank("bat", "NULL") = "bat" StringUtils.defaultIfBlank("", null) = null
defaultIfEmpty(T str, T defaultStr) 如果字符串為null、空(""),將返回指定字符串,否則返回原值
StringUtils.defaultIfEmpty(null, "NULL") = "NULL" StringUtils.defaultIfEmpty("", "NULL") = "NULL" StringUtils.defaultIfEmpty(" ", "NULL") = " " StringUtils.defaultIfEmpty("bat", "NULL") = "bat" StringUtils.defaultIfEmpty("", null) = null
defaultString(String str) 如果字符串為null,將返回空的字符串(""),否則返回原值
StringUtils.defaultString(null) = "" StringUtils.defaultString("") = "" StringUtils.defaultString("bat") = "bat"
defaultString(String str, String defaultStr) 如果字符串為null,將返回指定字符,否則返回原值
StringUtils.defaultString(null, "NULL") = "NULL" StringUtils.defaultString("", "NULL") = "" StringUtils.defaultString("bat", "NULL") = "bat"
deleteWhitespace(String str) 刪除字符串中的空格字符,並返回新的字符串
StringUtils.deleteWhitespace(null) = null StringUtils.deleteWhitespace("") = "" StringUtils.deleteWhitespace("abc") = "abc" StringUtils.deleteWhitespace(" ab c ") = "abc"
difference(String str1, String str2) 比較兩個字符串差異,並返回差異的字符,返回第二個字符串的剩余部分,這意味着“ABC”和“AB”之間的區別是空字符串而不是“C”。
StringUtils.difference(null, null) = null StringUtils.difference("", "") = "" StringUtils.difference("", "abc") = "abc" StringUtils.difference("abc", "") = "" StringUtils.difference("abc", "abc") = "" StringUtils.difference("abc", "ab") = "" StringUtils.difference("ab", "abxyz") = "xyz" StringUtils.difference("abcde", "abxyz") = "xyz" StringUtils.difference("abcde", "xyz") = "xyz"
endsWith(CharSequence str, CharSequence suffix) 檢查字符串是否以指定字符結尾,返回一個boolean
StringUtils.endsWith(null, null) = true StringUtils.endsWith(null, "def") = false StringUtils.endsWith("abcdef", null) = false StringUtils.endsWith("abcdef", "def") = true StringUtils.endsWith("ABCDEF", "def") = false StringUtils.endsWith("ABCDEF", "cde") = false StringUtils.endsWith("ABCDEF", "") = true
endsWithAny(CharSequence sequence, CharSequence... searchStrings) 檢查字符串是否以指定字符數組結尾,返回一個boolean
StringUtils.endsWithAny(null, null) = false StringUtils.endsWithAny(null, new String[] {"abc"}) = false StringUtils.endsWithAny("abcxyz", null) = false StringUtils.endsWithAny("abcxyz", new String[] {""}) = true StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true StringUtils.endsWithAny("abcXYZ", "def", "XYZ") = true StringUtils.endsWithAny("abcXYZ", "def", "xyz") = false
endsWithIgnoreCase(CharSequence str, CharSequence suffix) 檢查字符串是否以指定字符(不區分大小寫)結尾,返回一個boolean
StringUtils.endsWithIgnoreCase(null, null) = true StringUtils.endsWithIgnoreCase(null, "def") = false StringUtils.endsWithIgnoreCase("abcdef", null) = false StringUtils.endsWithIgnoreCase("abcdef", "def") = true StringUtils.endsWithIgnoreCase("ABCDEF", "def") = true StringUtils.endsWithIgnoreCase("ABCDEF", "cde") = false
equals(CharSequence cs1, CharSequence cs2) 比較兩個字符串是否相等,返回一個boolean
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
equalsAnyIgnoreCase(CharSequence string, CharSequence... searchStrings) 比較兩個字符串是否相等(不區分大小寫),返回一個boolean
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
equalsAny(CharSequence string, CharSequence... searchStrings) 比較字符串是否與指定字符串數組中某一值相等,返回一個boolean
StringUtils.equalsAny(null, (CharSequence[]) null) = false StringUtils.equalsAny(null, null, null) = true StringUtils.equalsAny(null, "abc", "def") = false StringUtils.equalsAny("abc", null, "def") = false StringUtils.equalsAny("abc", "abc", "def") = true StringUtils.equalsAny("abc", "ABC", "DEF") = false
equalsAnyIgnoreCase(CharSequence string, CharSequence... searchStrings) 比較字符串是否與指定字符串數組中某一值相等(不區分大小寫),返回一個boolean
StringUtils.equalsAnyIgnoreCase(null, (CharSequence[]) null) = false StringUtils.equalsAnyIgnoreCase(null, null, null) = true StringUtils.equalsAnyIgnoreCase(null, "abc", "def") = false StringUtils.equalsAnyIgnoreCase("abc", null, "def") = false StringUtils.equalsAnyIgnoreCase("abc", "abc", "def") = true StringUtils.equalsAnyIgnoreCase("abc", "ABC", "DEF") = true
getCommonPrefix(String... strs) 獲取字符串數組元素公共字符,返回string
StringUtils.getCommonPrefix(null) = "" StringUtils.getCommonPrefix(new String[] {}) = "" StringUtils.getCommonPrefix(new String[] {"abc"}) = "abc" StringUtils.getCommonPrefix(new String[] {null, null}) = "" StringUtils.getCommonPrefix(new String[] {"", ""}) = "" StringUtils.getCommonPrefix(new String[] {"", null}) = "" StringUtils.getCommonPrefix(new String[] {"abc", null, null}) = "" StringUtils.getCommonPrefix(new String[] {null, null, "abc"}) = "" StringUtils.getCommonPrefix(new String[] {"", "abc"}) = "" StringUtils.getCommonPrefix(new String[] {"abc", ""}) = "" StringUtils.getCommonPrefix(new String[] {"abc", "abc"}) = "abc" StringUtils.getCommonPrefix(new String[] {"abc", "a"}) = "a" StringUtils.getCommonPrefix(new String[] {"ab", "abxyz"}) = "ab" StringUtils.getCommonPrefix(new String[] {"abcde", "abxyz"}) = "ab" StringUtils.getCommonPrefix(new String[] {"abcde", "xyz"}) = "" StringUtils.getCommonPrefix(new String[] {"xyz", "abcde"}) = "" StringUtils.getCommonPrefix(new String[] {"i am a machine", "i am a robot"}) = "i am a "
indexOf(CharSequence seq, CharSequence searchSeq) 檢查指定字符在字符串中出現的位置,返回一個int值
StringUtils.indexOf(null, *) = -1 StringUtils.indexOf(*, null) = -1 StringUtils.indexOf("", "") = 0 StringUtils.indexOf("", *) = -1 (except when * = "") StringUtils.indexOf("aabaabaa", "a") = 0 StringUtils.indexOf("aabaabaa", "b") = 2 StringUtils.indexOf("aabaabaa", "ab") = 1 StringUtils.indexOf("aabaabaa", "") = 0
indexOfIgnoreCase(CharSequence seq, CharSequence searchSeq) 檢查指定字符在字符串中出現的位置(不區分大小寫),返回一個int值
isAllBlank(CharSequence... css) 檢查數組所有字符是否為null、empty、或全是空格字符,返回一個boolean
StringUtils.isAllBlank(null) = true StringUtils.isAllBlank(null, "foo") = false StringUtils.isAllBlank(null, null) = true StringUtils.isAllBlank("", "bar") = false StringUtils.isAllBlank("bob", "") = false StringUtils.isAllBlank(" bob ", null) = false StringUtils.isAllBlank(" ", "bar") = false StringUtils.isAllBlank("foo", "bar") = false StringUtils.isAllBlank(new String[] {}) = true
isAllEmpty(CharSequence... css) 檢查數組所有字符是否為null、empty,返回一個boolean
StringUtils.isAllEmpty(null) = true StringUtils.isAllEmpty(null, "") = true StringUtils.isAllEmpty(new String[] {}) = true StringUtils.isAllEmpty(null, "foo") = false StringUtils.isAllEmpty("", "bar") = false StringUtils.isAllEmpty("bob", "") = false StringUtils.isAllEmpty(" bob ", null) = false StringUtils.isAllEmpty(" ", "bar") = false StringUtils.isAllEmpty("foo", "bar") = false
isAllLowerCase(CharSequence cs) 檢查字符串中所有字符是否是小寫,返回一個boolean
isAllUpperCase(CharSequence cs) 檢查字符串中所有字符是否是大寫,返回一個boolean
isAnyBlank(CharSequence... css) 檢查數組中字符串是否有一個為null、empty或全是空格字符,返回一個boolean
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
isAnyEmpty(CharSequence... css) 檢查數組中字符串是否有一個為null、empty,返回一個boolean
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 StringUtils.isAnyEmpty(new String[]{}) = false StringUtils.isAnyEmpty(new String[]{""}) = true
isBlank(CharSequence cs) 檢查字符串是否為null、empty或空格字符,返回一個boolean
StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false
isEmpty(CharSequence cs) 檢查字符串是否為null、empty,返回一個boolean
StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false
isNotBlank(CharSequence cs) 檢查字符串是否不為null、empty或空格字符,返回一個boolean
isNotEmpty(CharSequence cs) 檢查字符串是否不為null、empty,返回一個boolean
isNumeric(CharSequence cs) 檢查字符串是否是數字,返回一個boolean
StringUtils.isNumeric(null) = false StringUtils.isNumeric("") = false StringUtils.isNumeric(" ") = false StringUtils.isNumeric("123") = true StringUtils.isNumeric("१२३") = true StringUtils.isNumeric("12 3") = false StringUtils.isNumeric("ab2c") = false StringUtils.isNumeric("12-3") = false StringUtils.isNumeric("12.3") = false StringUtils.isNumeric("-123") = false StringUtils.isNumeric("+123") = false
isWhitespace(CharSequence cs) 檢查字符串是否是空格字符,返回一個boolean
StringUtils.isWhitespace(null) = false StringUtils.isWhitespace("") = true StringUtils.isWhitespace(" ") = true StringUtils.isWhitespace("abc") = false StringUtils.isWhitespace("ab2c") = false StringUtils.isWhitespace("ab-c") = false
join(byte[] array, char separator) 將字節數組轉換成string,以指定字符分隔
StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join([1, 2, 3], ';') = "1;2;3" StringUtils.join([1, 2, 3], null) = "123" char、double、float、int、long、short、object、T同理
joinWith(String separator, Object... objects) 將多個元素已指定字符分隔拼接成String
StringUtils.joinWith(",", {"a", "b"}) = "a,b" StringUtils.joinWith(",", {"a", "b",""}) = "a,b," StringUtils.joinWith(",", {"a", null, "b"}) = "a,,b" StringUtils.joinWith(null, {"a", "b"}) = "ab"
lastIndexOf(CharSequence seq, CharSequence searchSeq) 獲取指定字符在字符串中的最后一個索引位置
StringUtils.lastIndexOf(null, *) = -1 StringUtils.lastIndexOf(*, null) = -1 StringUtils.lastIndexOf("", "") = 0 StringUtils.lastIndexOf("aabaabaa", "a") = 7 StringUtils.lastIndexOf("aabaabaa", "b") = 5 StringUtils.lastIndexOf("aabaabaa", "ab") = 4 StringUtils.lastIndexOf("aabaabaa", "") = 8
left(String str, int len) 返回從左邊開始指定大小的字符串
StringUtils.left(null, *) = null StringUtils.left(*, -ve) = "" StringUtils.left("", *) = "" StringUtils.left("abc", 0) = "" StringUtils.left("abc", 2) = "ab" StringUtils.left("abc", 4) = "abc"
right(String str, int len) 同上相反
length(CharSequence cs) 獲取字符串大小,返回一個int
lowerCase(String str) 將字符串轉換為小寫,返回一個string
StringUtils.lowerCase(null) = null StringUtils.lowerCase("") = "" StringUtils.lowerCase("aBc") = "abc"
upperCase(String str) 同上相反
mid(String str, int pos, int len) 獲取字符串指定位置區間的字符,返回一個string
StringUtils.mid(null, *, *) = null StringUtils.mid(*, *, -ve) = "" StringUtils.mid("", 0, *) = "" StringUtils.mid("abc", 0, 2) = "ab" StringUtils.mid("abc", 0, 4) = "abc" StringUtils.mid("abc", 2, 4) = "c" StringUtils.mid("abc", 4, 2) = "" StringUtils.mid("abc", -2, 2) = "ab"
overlay(String str, String overlay, int start, int end) 在字符串位置區間插入指定字符,返回一個string
StringUtils.overlay(null, *, *, *) = null StringUtils.overlay("", "abc", 0, 0) = "abc" StringUtils.overlay("abcdef", null, 2, 4) = "abef" StringUtils.overlay("abcdef", "", 2, 4) = "abef" StringUtils.overlay("abcdef", "", 4, 2) = "abef" StringUtils.overlay("abcdef", "zzzz", 2, 4) = "abzzzzef" StringUtils.overlay("abcdef", "zzzz", 4, 2) = "abzzzzef" StringUtils.overlay("abcdef", "zzzz", -1, 4) = "zzzzef" StringUtils.overlay("abcdef", "zzzz", 2, 8) = "abzzzz" StringUtils.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef" StringUtils.overlay("abcdef", "zzzz", 8, 10) = "abcdefzzzz"
prependIfMissing(String str, CharSequence prefix, CharSequence... prefixes) 在字符串最左邊插入指定字符,如果已存在,將不插入,返回一個string
StringUtils.prependIfMissing(null, null) = null StringUtils.prependIfMissing("abc", null) = "abc" StringUtils.prependIfMissing("", "xyz") = "xyz" StringUtils.prependIfMissing("abc", "xyz") = "xyzabc" StringUtils.prependIfMissing("xyzabc", "xyz") = "xyzabc" StringUtils.prependIfMissing("XYZabc", "xyz") = "xyzXYZabc"
prependIfMissingIgnoreCase(String str, CharSequence prefix, CharSequence... prefixes) 同上,只是不區分大小寫
remove(String str, char remove) 刪除字符串中指定字符,返回一個string
StringUtils.remove(null, *) = null StringUtils.remove("", *) = "" StringUtils.remove("queued", 'u') = "qeed" StringUtils.remove("queued", 'z') = "queued"
removeIgnoreCase(String str, String remove) 同上,只是不區分大小寫
removeAll(String text, String regex) 根據匹配規則刪除所有字符,返回一個string
StringUtils.removeAll(null, *) = null StringUtils.removeAll("any", null) = "any" StringUtils.removeAll("any", "") = "any" StringUtils.removeAll("any", ".*") = "" StringUtils.removeAll("any", ".+") = "" StringUtils.removeAll("abc", ".?") = "" StringUtils.removeAll("A<__>\n<__>B", "<.*>") = "A\nB" StringUtils.removeAll("A<__>\n<__>B", "(?s)<.*>") = "AB" StringUtils.removeAll("ABCabc123abc", "[a-z]") = "ABC123"
removeEnd(String str, String remove) 刪除字符串結尾指定字符,返回一個string
StringUtils.removeEnd(null, *) = null StringUtils.removeEnd("", *) = "" StringUtils.removeEnd(*, null) = * 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.removeEnd("abc", "") = "abc"
removeStart(String str, String remove) 同上相反
removeEndIgnoreCase(String str, String remove) 同上,只是不區分大小寫
removeFirst(String text, String regex) 根據匹配規則刪除第一次出現的字符,返回一個string
StringUtils.removeFirst(null, *) = null StringUtils.removeFirst("any", null) = "any" StringUtils.removeFirst("any", "") = "any" StringUtils.removeFirst("any", ".*") = "" StringUtils.removeFirst("any", ".+") = "" StringUtils.removeFirst("abc", ".?") = "bc" StringUtils.removeFirst("A<__>\n<__>B", "<.*>") = "A\n<__>B" StringUtils.removeFirst("A<__>\n<__>B", "(?s)<.*>") = "AB" StringUtils.removeFirst("ABCabc123", "[a-z]") = "ABCbc123" StringUtils.removeFirst("ABCabc123abc", "[a-z]+") = "ABC123abc"
repeat(String str, int repeat) 將字符重復指定次數拼接成新的字符串,返回一個string
StringUtils.repeat(null, 2) = null StringUtils.repeat("", 0) = "" StringUtils.repeat("", 2) = "" StringUtils.repeat("a", 3) = "aaa" StringUtils.repeat("ab", 2) = "abab" StringUtils.repeat("a", -2) = ""
replace(String text, String searchString, String replacement) 用replacement替換字符串中的所有searchString,返回一個string
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"
reverse(String str) 將字符串反轉,返回一個string
StringUtils.reverse(null) = null StringUtils.reverse("") = "" StringUtils.reverse("bat") = "tab"
reverseDelimited(String str, char separatorChar) 將字符串指定分隔符出的字符反轉
StringUtils.reverseDelimited(null, *) = null StringUtils.reverseDelimited("", *) = "" StringUtils.reverseDelimited("a.b.c", 'x') = "a.b.c" StringUtils.reverseDelimited("a.b.c", ".") = "c.b.a"
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"]
substring(String str, int start) 將字符串從指定位置區間截取,返回string
swapCase(String str) 將字符串大小寫互轉,返回一個string
StringUtils.swapCase(null) = null StringUtils.swapCase("") = "" StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
toEncodedString(byte[] bytes, Charset charset) 將字符串轉為指定編碼格式,返回一個string
trim(String str) 去除字符串空格
trimToEmpty(String str) 去除字符串空格,null轉為empty,返回一個string
StringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc"