Apache StringUtils源碼詳解

Apache StringUtils源碼詳解

1、成員變量

  • SPACE = " ",空白字符串
  • EMPTY="",空字符串
  • LF="\n",換行
  • CR="\n",回車
  • int INDEX_NOT_FOUND=-1,字符串查找未找到時返回的值
  • int PAD_LIMIT=8192

回車和換行的區別?

回車會回到本行的行首,換行才會到下一行行首

2、方法

2.1 isEmpty(final CharSequence cs)
public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

該方法判斷輸入的字符序列是否為空,當字符序列為null或者長度為0時返回true,其他情況返回false。

需要注意的是空白字符序列不為空。

2.2 isNotEmpty(final CharSequence cs)
public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

該方法判斷輸入的字符序列是否不為空,取值與isEmpty相反。

2.3 isAnyEmpty(final CharSequence... css)
public static boolean isAnyEmpty(final CharSequence... css) {
      if (ArrayUtils.isEmpty(css)) {
        return true;
      }
      for (final CharSequence cs : css){
        if (isEmpty(cs)) {
          return true;
        }
      }
      return false;
    }

該方法判斷一個字符序列數組中是否有任意一個為空,是則返回true,否則返回false。

2.4 isNoneEmpty(final CharSequence... css)
public static boolean isNoneEmpty(final CharSequence... css) {
      return !isAnyEmpty(css);
    } 

判斷字符序列數組是否都不為空。

2.5 isBlank(final CharSequence cs)
public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

判斷輸入字符序列是否為空白,所謂的空白字符串包括三種情況

  • 空串
  • 只包含空白字符的串
  • null
2.6 isNotBlank(final CharSequence cs)
public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

判斷輸入字符序列是否非空白,返回值與2.5相反。

2.7 isAnyBlank(final CharSequence... css)
public static boolean isAnyBlank(final CharSequence... css) {
      if (ArrayUtils.isEmpty(css)) {
        return true;
      }
      for (final CharSequence cs : css){
        if (isBlank(cs)) {
          return true;
        }
      }
      return false;
    }

判斷輸入的字符序列數組是否有任意一個為空白。

2.8 isNoneBlank(final CharSequence... css)
public static boolean isNoneBlank(final CharSequence... css) {
      return !isAnyBlank(css);
    }

判斷輸入的字符序列數組是否都不為空白

2.9 trim(final String str)
public static String trim(final String str) {
        return str == null ? null : str.trim();
    }

移除輸入字符序列的首尾空白字符。如果輸入字符序列為null,則返回null。

2.10 trimToNull(final String str)
 public static String trimToNull(final String str) {
        final String ts = trim(str);
        return isEmpty(ts) ? null : ts;
    }

移除輸入字符序列首尾空白字符,如果移除后的字符序列為空(""或者null),則返回null,否則返回移除后的字符串。

2.11 trimToEmpty(final String str)
public static String trimToEmpty(final String str) {
        return str == null ? EMPTY : str.trim();
    }

如果輸入字符序列為null,則返回"",否則返回移除首尾空白字符的結果。

2.12 strip(final String str)
public static String strip(final String str) {
        return strip(str, null);
    }

其效果等同于trim。

2.13 stripToNull(String str)
public static String stripToNull(String str) {
        if (str == null) {
            return null;
        }
        str = strip(str, null);
        return str.isEmpty() ? null : str;
    }

其效果等同于trimToNull。

2.14 stripToEmpty(final String str)
public static String stripToEmpty(final String str) {
        return str == null ? EMPTY : strip(str, null);
    }

其效果等同于trimToEmpty。

2.15 String stripStart(final String str, final String stripChars)

移除str首部任何stripChars中的任意字符。stripChars=null時,視為""處理。

  • StringUtils.stripStart(null, *) = null
  • StringUtils.stripStart("", *) = ""
  • StringUtils.stripStart("abc", "") = "abc"
  • StringUtils.stripStart("abc", null) = "abc"
  • StringUtils.stripStart(" abc", null) = "abc"
  • StringUtils.stripStart("abc ", null) = "abc "
  • StringUtils.stripStart(" abc ", null) = "abc "
  • StringUtils.stripStart("yxabc ", "xyz") = "abc "
2.16 String stripEnd(final String str, final String stripChars)

作用跟2.15類似,只不過是從尾部移除。

  • StringUtils.stripEnd(null, *) = null
  • StringUtils.stripEnd("", *) = ""
  • StringUtils.stripEnd("abc", "") = "abc"
  • StringUtils.stripEnd("abc", null) = "abc"
  • StringUtils.stripEnd(" abc", null) = " abc"
  • StringUtils.stripEnd("abc ", null) = "abc"
  • StringUtils.stripEnd(" abc ", null) = " abc"
  • StringUtils.stripEnd(" abcyx", "xyz") = " abc"
  • StringUtils.stripEnd("120.00", ".0") = "12"
2.17 stripAll(final String... strs)
String[] stripAll(final String... strs) {
        return stripAll(strs, null);
    }

對于數組中的每一個字符串調用strip(String)方法。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 開心一笑 【男女朋友一起去逛街,女朋友:哎喲,腳好酸哦。男朋友很緊張:怎么了?是不是踩到檸檬了?】 提出問題 La...
    架構師啟示錄閱讀 6,871評論 8 29
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,933評論 18 139
  • StringUtils字符串工具類的常用方法 一、StringUtils是什么 StringUtils 方法的操作...
    天下無憂2000閱讀 11,774評論 0 5
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,765評論 18 399
  • 此刻,特別想書寫點什么,抒發點什么,記錄點什么,對,是一個里程碑!必須記錄。 看得到通往財富自由之路,受益匪淺,甚...
    平行夏閱讀 203評論 0 0