commons-lang3之StringUtils
字符串是一種在開發(fā)中經(jīng)常使用到的數(shù)據(jù)類型,對字符串的處理也變得非常重要,字符串本身有一些方法,但都沒有對null做處理,而且有時(shí)可能還需要做一些額外處理才能滿足我們的需求,比如,要判斷某個(gè)字符串中是否包含字符串a(chǎn)或者字符串a(chǎn)x,使用自帶的字符串方法,我們可能要這么寫
boolean isContains = false; String s = "abc"; if(s != null) {if(s.contains("a") || s.contains("ax")) {isContains = true;} }使用commons-lang3工具包,就只需要一行代碼就可以,其他內(nèi)容已經(jīng)被封裝好,并且已經(jīng)對null做了處理,StringUtils類中大部分的方法都對null做了處理,所以不會出現(xiàn)空指針異常
boolean flag = StringUtils.containsAny("abc", new String[] {"a","ax"});但即便如此,對于第三方j(luò)ar包,也建議不要直接使用,最好自己封裝一下,使用時(shí)調(diào)用自己封裝的接口,這樣,以后如果方法有變動,或者使用其他的jar包,也不需要每個(gè)調(diào)用都做修改,只需要修改自己封裝的接口即可。不要使用已過期的方法
maven依賴如:https://www.cnblogs.com/qq931399960/p/10689571.html中所示,可以簡單先看下這個(gè)類中的方法,有個(gè)印象,在對字符串做處理時(shí),再去源碼中查看具體的使用方式,每個(gè)方法在注釋中都有詳細(xì)的例子,使用起來很方便,比如上述containsAny方法
/*** <p>Checks if the CharSequence contains any of the CharSequences in the given array.</p>** <p>* A {@code null} {@code cs} CharSequence will return {@code false}. A {@code null} or zero* length search array will return {@code false}.* </p>** <pre>* 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* </pre>*** @param cs The CharSequence to check, may be null* @param searchCharSequences The array of CharSequences to search for, may be null.* Individual CharSequences may be null as well.* @return {@code true} if any of the search CharSequences are found, {@code false} otherwise* @since 3.4*/public static boolean containsAny(final CharSequence cs, final CharSequence... searchCharSequences) {if (isEmpty(cs) || ArrayUtils.isEmpty(searchCharSequences)) {return false;}for (final CharSequence searchCharSequence : searchCharSequences) {if (contains(cs, searchCharSequence)) {return true;}}return false;}下面是對StringUtils的一些簡單測試
// true,為null或者size==0,返回trueboolean empty = StringUtils.isEmpty("");// false,與isEmpty相反boolean notEmpty = StringUtils.isNotEmpty("");// true,數(shù)組中有一個(gè)為null或size==0的字符串,則返回trueboolean anyEmpty = StringUtils.isAnyEmpty(new String[] { "aa", "" });// false,全部都不為空,返回trueboolean noneEmpty = StringUtils.isNoneEmpty(new String[] { "", "aa" });// true,全部為空,返回trueboolean allEmpty = StringUtils.isAllEmpty(new String[] { "", "" });// true,為null或者size==0或者只存在空白字符(如" "),則返回trueboolean blank = StringUtils.isBlank(" ");// false,與isBlank相反boolean notBlank = StringUtils.isNotBlank(" ");// true,數(shù)組中任何一個(gè)為null或者空字符串或者空白字符,則返回trueboolean anyBlank = StringUtils.isAnyBlank(new String[] { "dd", " " });// false,與isAnyBlank 相反boolean noneBlank = StringUtils.isNoneBlank(new String[] { "dd", "" });// true,數(shù)組中的數(shù)據(jù)全部為null,或者空字符串或者空白字符,則返回trueboolean allBlank = StringUtils.isAllBlank(new String[] { "", " " });// dd,去掉前后字符,為null,返回nullString trim = StringUtils.trim(" dd ");// "",為null或者size==0,則返回空字符串String trimToEmpty = StringUtils.trimToEmpty(" ");// null,為null或者size==0,則返回nullString trimToNull = StringUtils.trimToNull(" ");// abc,截取字符串String truncate = StringUtils.truncate("abcde", 3);// cdefg,從第二個(gè)起,截取5個(gè)長度String truncate = StringUtils.truncate("abcdefghl", 2, 5);// ddds與trim類似String strip = StringUtils.strip(" dd d s ");// yes, it is,去掉指定的開頭字符和結(jié)尾字符,第二個(gè)字符為dd或者da也會有同樣輸出String strip = StringUtils.strip("ddyes, it is ddd", "d");// yes, it is dddString stripStart = StringUtils.stripStart("ddyes, it is ddd", "d");// ddyes, it isString stripEnd = StringUtils.stripEnd("ddyes, it is ddd", "d");// [it is, dd],去掉參數(shù)中所有元素的前后空格String[] stripAll = StringUtils.stripAll(" it is ", " dd ");// [it is , it],去掉數(shù)組中每個(gè)元素前后的指定字符String[] stripAll = StringUtils.stripAll(new String[] { "ddit is d", "ditd" }, "d");// falseboolean equals = StringUtils.equals(null, "");// trueboolean equalsIgnoreCase = StringUtils.equalsIgnoreCase("abc", "ABC");// [,ab,cde, dsd,] 2個(gè)元素,默認(rèn)分隔符為空白字符String[] split = StringUtils.split(",ab,cde dsd,");// [ab, cde , dsd] 3個(gè)元素,去掉了為空的元素,空白字符組成的字符串會保留String[] split = StringUtils.split(",ab,cde ,,dsd,", ",");// [ab, cde, dsd] 3個(gè)元素,以,和空白字符分隔,第二個(gè)參數(shù)中每個(gè)字符都當(dāng)成一個(gè)分隔符,與上一個(gè)方法比,放方法第二個(gè)元素后沒有空白字符String[] split = StringUtils.split(",ab,cde ,,dsd,", ", ");// [ab, cde ,,dsd,] 2個(gè)元素,由于最大只允許2個(gè)String[] split = StringUtils.split(",ab,cde ,,dsd,", ", ", 2);// [,ab,cde , ,dsd,] 2個(gè)元素,第二個(gè)參數(shù)所有字符當(dāng)成一個(gè)整體的分隔符String[] splitByWholeSeparator = StringUtils.splitByWholeSeparator(",ab,cde , ,dsd,", ", ");// [, dd, ],3個(gè)元素,兩個(gè)空的String[] splitPreserveAllTokens = StringUtils.splitPreserveAllTokens(" dd ");// [, aa, , ],4個(gè)元素,3個(gè)空的String[] splitPreserveAllTokens = StringUtils.splitPreserveAllTokens(",aa,,", ",");// [, aa, , bb, ] 5個(gè)元素,以,或者空白字符分隔String[] splitPreserveAllTokens = StringUtils.splitPreserveAllTokens(",aa, bb,", ", ");// [,aa, bb,] 2個(gè),以, 作為一個(gè)整體分隔String[] splitByWholeSeparatorPreserveAllTokens = StringUtils.splitByWholeSeparatorPreserveAllTokens(",aa, bb,",", ");// [ABC, 123, abc, ;,., I, t] 6個(gè)元素String[] splitByCharacterType = StringUtils.splitByCharacterType("ABC123abc;,.It");String join = StringUtils.join("12", "a", "df", "3"); // 12adf3List<String> ls = new ArrayList<>();ls.add("aa");ls.add("bb");ls.add("cc");// insert into table values ('aa','bb','cc');,在組織'aa','bb','cc'String join = "'" + StringUtils.join(ls, "','") + "'";// abcString deleteWhitespace = StringUtils.deleteWhitespace(" a b c ");// abc is itString remove = StringUtils.remove("abcdd is dddit", "d");// dit isdd,只刪除第一個(gè)參數(shù)起始處中對應(yīng)的第二個(gè)參數(shù)。String removeStart = StringUtils.removeStart("ddit isdd", "d");// ddit isd, 只刪除第一個(gè)參數(shù)結(jié)束處中對應(yīng)的第二個(gè)參數(shù)。String removeEnd = StringUtils.removeEnd("ddit isdd", "d");// it isString removeIgnoreCase = StringUtils.removeIgnoreCase("ddit isdd", "D");// rdit// isdd,雖然replace的功能已經(jīng)包含有replaceOne,但如果確定只有一個(gè)需要替換,最好還是使用replaceOne,因?yàn)檫@個(gè)找到一個(gè)替換后就會停止查找String replaceOnce = StringUtils.replaceOnce("ddit isdd", "d", "r");// rdit isddString replaceOnceIgnoreCase = StringUtils.replaceOnceIgnoreCase("ddit isdd", "D", "r");// rrit isrrString replace = StringUtils.replace("ddit isdd", "d", "r");// rrit isrrString replaceIgnoreCase = StringUtils.replaceIgnoreCase("ddit isdd", "D", "r");// rris isrr,批量替換String replaceEach = StringUtils.replaceEach("ddit isdd", new String[] { "d", "t" }, new String[] { "r", "s" });// tcteString replaceEachRepeatedly = StringUtils.replaceEachRepeatedly("abcde", new String[] { "ab", "d" },new String[] { "d", "t" });// aaaaaa 將第一個(gè)參數(shù)重復(fù)第二個(gè)參數(shù)次,形成一個(gè)新的字符串String repeat = StringUtils.repeat("aa", 3);// aa,aa,aa 將第一個(gè)參數(shù)重復(fù)第三個(gè)參數(shù)次,并且以第二個(gè)參數(shù)分隔,形成一個(gè)新的字符串String repeat = StringUtils.repeat("aa", ",", 3);// ab,在左側(cè)填充兩個(gè)空白字符,形成一個(gè)4個(gè)字符的字符串,String leftPad = StringUtils.leftPad("ab", 4);// ab ,在兩邊填充空白字符,形成一個(gè)以第一個(gè)參數(shù)為中心的4個(gè)字符串長度字符串String center = StringUtils.center("ab", 4);// true,正整數(shù)返回trueboolean numeric = StringUtils.isNumeric("123");// true 正整數(shù),且包含有空白字符或者空字符串,空白字符,返回trueboolean numericSpace = StringUtils.isNumericSpace("12 3");// 5417543010,獲取字符串中的數(shù)字,并拼接在一起String digits = StringUtils.getDigits("(541) 754-3010");// true,字符串size==0或者空白字符,返回true,null及其他字符返回falseboolean whitespace = StringUtils.isWhitespace(" ");// abcdefg...,顯示指定長度的字符串,多余的使用...String abbreviate = StringUtils.abbreviate("abcdefghijklmnopq", 10);// abcdefg***String abbreviate = StringUtils.abbreviate("abcdefghijklmnopq", "***", 10);// abcd***opqString abbreviateMiddle = StringUtils.abbreviateMiddle("abcdefghijklmnopq", "***", 10);// a,獲取數(shù)組共所有元素相同的前綴String commonPrefix = StringUtils.getCommonPrefix("abcdf", "ads", "arf");// true, endsWith同理boolean startsWith = StringUtils.startsWith("abcdef", "ab");// true,endsWithIgnoreCase同理boolean startsWithIgnoreCase = StringUtils.startsWithIgnoreCase("abcdefg", "aB");// true,第一個(gè)參數(shù)的前綴匹配第二個(gè)數(shù)組參數(shù)中的任何一個(gè)元素時(shí),返回true,endsWithAny同理boolean startsWithAny = StringUtils.startsWithAny("abcdef", new String[] { "x", "ab", " " });// abcxyzddd,如果出第一個(gè)參數(shù)的后綴包含在后面參數(shù)中,則直接返回第一個(gè)參數(shù),否則將返回第一個(gè)參數(shù)+第二個(gè)參數(shù)String appendIfMissing = StringUtils.appendIfMissing("abcxyz", "ddd", new String[] { "qwe", "123" });// dddabcxyz,原理同上String prependIfMissing = StringUtils.prependIfMissing("abcxyz", "ddd", new String[] { "qwe", "123" });// cbdString encodedString = StringUtils.toEncodedString(new byte[] { 'c', 'b', 'd' }, Charset.defaultCharset());// "xxx"String wrap = StringUtils.wrap("xxx", "\"");// *xx*String wrapIfMissing = StringUtils.wrapIfMissing("xx", "*");// 前后必須有相同字符才可以unwrapString unwrap = StringUtils.unwrap("'aa'", "'");// {97,98,99}int[] codePoints = StringUtils.toCodePoints("abc");// abc,刪除最后一個(gè)字符,如果是\r\n則一起刪除String chop = StringUtils.chop("abc\r\n");// abc,如果最后存在換行符,則刪除最后的換行符String chomp = StringUtils.chomp("abc\r\n");// trueboolean contains = StringUtils.contains("abcd", "ab");// trueboolean containsAny = StringUtils.containsAny("abcdefg", "2", "a");// falseboolean containsOnly = StringUtils.containsOnly("abcdefg", "aa");// trueboolean containsIgnoreCase = StringUtils.containsIgnoreCase("abcdef", "aB");// falseboolean containsNone = StringUtils.containsNone("abcdef", 'a', 'x');// trueboolean containsWhitespace = StringUtils.containsWhitespace("aa bbc");// 此外還有substring,indexof等方法 View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/qq931399960/p/10697895.html
總結(jié)
以上是生活随笔為你收集整理的commons-lang3之StringUtils的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql-增删改(DML)
- 下一篇: 第一天开始学习使用git中遇到的问题