apache-commons-lang3之StringUtils使用集錦
一.前言
1.CharSequence
StringUtils是處理字符串的工具類, String類實現CharSequence接口,為兼容不同實現類,StringUtils在處理字符串的傳參為CharSequence類型
二.方法
1.判定是否為空
判定是否為空,包括是否為null,空串,空格字符串等,同時傳參時區分單一參數與不定長參數集,其中empty結尾的函數校驗null和空串,blank加上了對於空格字符串一類的無意義字符串的校驗.
2.trim函數
StringUtils.trim(final String str){} 字符串修建函數trim()在String類自帶trim函數的基礎上,新增了對於null的支持,傳參為null返回null,非空字符串的操作即去除首尾空格的一般操作,並基於此衍生除兩個函數trimToNull -- 空串返回null 和 trimToEmpty -- null返回空串.
3.truncate函數
截取函數truncate基於String類的substring方法實現,變更了傳參邏輯,String類的substring函數傳參起始索引與終止索引,StringUtils工具類的truncate函數則傳參根字符串/起始索引(偏移量)/截取長度,更多了一種截取選擇,適應不同的使用場景,源碼如下:
public static String truncate(final String str, final int offset, final int maxWidth) { if (offset < 0) { throw new IllegalArgumentException("offset cannot be negative"); } if (maxWidth < 0) { throw new IllegalArgumentException("maxWith cannot be negative"); } if (str == null) { return null; } if (offset > str.length()) { return EMPTY; } if (str.length() > maxWidth) { final int ix = offset + maxWidth > str.length() ? str.length() : offset + maxWidth; return str.substring(offset, ix); } return str.substring(offset); }
4.strip函數
strip函數與trim函數同屬於字符串修剪方法實現, 不同在於trim處理字符串,strip可以對具體字符進行首尾的修剪,核心還是String類的substring函數,核心代碼位於stripStart和stripEnd函數.
public static String stripStart(final String str, final String stripChars) { int strLen; if (str == null || (strLen = str.length()) == 0) { return str; } int start = 0; if (stripChars == null) { while (start != strLen && Character.isWhitespace(str.charAt(start))) { start++; } } else if (stripChars.isEmpty()) { return str; } else { while (start != strLen && stripChars.indexOf(str.charAt(start)) != INDEX_NOT_FOUND) { start++; } } return str.substring(start); }
public static String stripEnd(final String str, final String stripChars) { int end; if (str == null || (end = str.length()) == 0) { return str; } if (stripChars == null) { while (end != 0 && Character.isWhitespace(str.charAt(end - 1))) { end--; } } else if (stripChars.isEmpty()) { return str; } else { while (end != 0 && stripChars.indexOf(str.charAt(end - 1)) != INDEX_NOT_FOUND) { end--; } } return str.substring(0, end); }
5.equals函數
比較字符串是否相等,核心是CharSequenceUtils.regionMatch函數,逐字符比較,可區分大小寫,出現不同即返回,包括equals和equalsIgnoreCase兩種實現,以及拓展的對於不定長參數集equalsAny/equalsAnyIgnoreCase的支持.
6.compare函數
比較兩個字符串,可區分大小寫,核心邏輯為String.compareTo函數, 比較編碼大小,返回第一個不同的差值,相同則返回0,整體業務邏輯類似equals,同時包含對於空參的支持,具體實現為compare和compareIgnoreCase
7.indexOf函數
獲取字符串中子串索引,核心邏輯為CharSequenceUtils.indexOf(seq, searchChar, startPos)函數,此處包含場景比較多,包括從字符串起始位置/指定位置,順序查找/逆序查找,簡列如下:
indexOf(final CharSequence seq, final int searchChar / final CharSequence serchSeq)
-- 獲取字符(ASCII編碼)位於字符串起始開始的第一個位置索引
indexOf(final CharSequence seq, final int searchChar / final CHarSequence searchSeq, final int startPos)
-- 獲取字符(ASCII編碼)位於字符串指定起始位置開始的第一個位置索引
8.contains函數
字符串參數間的包含關系,核心邏輯為CharSequenceUtils.indexOf(seq, searchChar, startPos)函數,如果子串在字符串的索引不小於零,則包含;對於空格的包含containsWhitespace則是遍歷字符串逐一判定.
public static boolean contains(final CharSequence seq, final CharSequence searchSeq) { if (seq == null || searchSeq == null) { return false; } return CharSequenceUtils.indexOf(seq, searchSeq, 0) >= 0; }
9.substring函數
StringUtils工具類對substring函數的應用場景足夠細化,可應用多種場景,除開3,4的truncate和strip,另有細化的使用場景,例如首部/尾部/中間等位置開始截取的left/right/mid,指定子串位置開始的截取,包括substringBefore/substringAfter/substringBeforeLast/substringAfterLast/...