1. 程式人生 > >阿帕奇,StringUtil工具類,各種常用字串的處理

阿帕奇,StringUtil工具類,各種常用字串的處理

功能:

1.判斷類(包括NULL和空串、是否是空白字串等)

2.預設值

3.去空白(trim)

4.比較

5.字元型別判斷(是否只包含數字、字母)

6.大小寫轉換(首字母大小寫等)

7.字串分割

8.字串連線

9.字串查詢

10.取子串

11.刪除字元

12.字串比較

/**
  * 有關字串處理的工具類。
  *
  * <p>
  * 這個類中的每個方法都可以“安全”地處理<code>null</code>,而不會丟擲<code>NullPointerException</code>。
  * </p>
  *
  */
 public class
StringUtil {
/* ============================================================================ */ /* 常量和singleton。 */ /* ============================================================================ */ /** 空字串。 */ public static final String EMPTY_STRING = ""
; /* ============================================================================ */ /* 判空函式。 */ /* */ /* 以下方法用來判定一個字串是否為: */
/* 1. null */ /* 2. empty - "" */ /* 3. blank - "全部是空白" - 空白由Character.isWhitespace所定義。 */ /* ============================================================================ */ /** * 檢查字串是否為<code>null</code>或空字串<code>""</code>。 * <pre> * StringUtil.isEmpty(null) = true * StringUtil.isEmpty("") = true * StringUtil.isEmpty(" ") = false * StringUtil.isEmpty("bob") = false * StringUtil.isEmpty(" bob ") = false * </pre> * * @param str 要檢查的字串 * * @return 如果為空, 則返回<code>true</code> */ public static boolean isEmpty(String str) { return ((str == null) || (str.length() == 0)); } /** * 檢查字串是否不是<code>null</code>和空字串<code>""</code>。 * <pre> * StringUtil.isEmpty(null) = false * StringUtil.isEmpty("") = false * StringUtil.isEmpty(" ") = true * StringUtil.isEmpty("bob") = true * StringUtil.isEmpty(" bob ") = true * </pre> * * @param str 要檢查的字串 * * @return 如果不為空, 則返回<code>true</code> */ public static boolean isNotEmpty(String str) { return ((str != null) && (str.length() > 0)); } /** * 檢查字串是否是空白:<code>null</code>、空字串<code>""</code>或只有空白字元。 * <pre> * StringUtil.isBlank(null) = true * StringUtil.isBlank("") = true * StringUtil.isBlank(" ") = true * StringUtil.isBlank("bob") = false * StringUtil.isBlank(" bob ") = false * </pre> * * @param str 要檢查的字串 * * @return 如果為空白, 則返回<code>true</code> */ public static boolean isBlank(String str) { int length; if ((str == null) || ((length = str.length()) == 0)) { return true; } for (int i = 0; i < length; i++) { if (!Character.isWhitespace(str.charAt(i))) { return false; } } return true; } /** * 檢查字串是否不是空白:<code>null</code>、空字串<code>""</code>或只有空白字元。 * <pre> * StringUtil.isBlank(null) = false * StringUtil.isBlank("") = false * StringUtil.isBlank(" ") = false * StringUtil.isBlank("bob") = true * StringUtil.isBlank(" bob ") = true * </pre> * * @param str 要檢查的字串 * * @return 如果為空白, 則返回<code>true</code> */ public static boolean isNotBlank(String str) { int length; if ((str == null) || ((length = str.length()) == 0)) { return false; } for (int i = 0; i < length; i++) { if (!Character.isWhitespace(str.charAt(i))) { return true; } } return false; } /* ============================================================================ */ /* 預設值函式。 */ /* */ /* 當字串為null、empty或blank時,將字串轉換成指定的預設字串。 */ /* ============================================================================ */ /** * 如果字串是<code>null</code>,則返回空字串<code>""</code>,否則返回字串本身。 * <pre> * StringUtil.defaultIfNull(null) = "" * StringUtil.defaultIfNull("") = "" * StringUtil.defaultIfNull(" ") = " " * StringUtil.defaultIfNull("bat") = "bat" * </pre> * * @param str 要轉換的字串 * * @return 字串本身或空字串<code>""</code> */ public static String defaultIfNull(String str) { return (str == null) ? EMPTY_STRING : str; } /** * 如果字串是<code>null</code>,則返回指定預設字串,否則返回字串本身。 * <pre> * StringUtil.defaultIfNull(null, "default") = "default" * StringUtil.defaultIfNull("", "default") = "" * StringUtil.defaultIfNull(" ", "default") = " " * StringUtil.defaultIfNull("bat", "default") = "bat" * </pre> * * @param str 要轉換的字串 * @param defaultStr 預設字串 * * @return 字串本身或指定的預設字串 */ public static String defaultIfNull(String str, String defaultStr) { return (str == null) ? defaultStr : str; } /** * 如果字串是<code>null</code>或空字串<code>""</code>,則返回空字串<code>""</code>,否則返回字串本身。 * * <p> * 此方法實際上和<code>defaultIfNull(String)</code>等效。 * <pre> * StringUtil.defaultIfEmpty(null) = "" * StringUtil.defaultIfEmpty("") = "" * StringUtil.defaultIfEmpty(" ") = " " * StringUtil.defaultIfEmpty("bat") = "bat" * </pre> * </p> * * @param str 要轉換的字串 * * @return 字串本身或空字串<code>""</code> */ public static String defaultIfEmpty(String str) { return (str == null) ? EMPTY_STRING : str; } /** * 如果字串是<code>null</code>或空字串<code>""</code>,則返回指定預設字串,否則返回字串本身。 * <pre> * StringUtil.defaultIfEmpty(null, "default") = "default" * StringUtil.defaultIfEmpty("", "default") = "default" * StringUtil.defaultIfEmpty(" ", "default") = " " * StringUtil.defaultIfEmpty("bat", "default") = "bat" * </pre> * * @param str 要轉換的字串 * @param defaultStr 預設字串 * * @return 字串本身或指定的預設字串 */ public static String defaultIfEmpty(String str, String defaultStr) { return ((str == null) || (str.length() == 0)) ? defaultStr : str; } /** * 如果字串是空白:<code>null</code>、空字串<code>""</code>或只有空白字元,則返回空字串<code>""</code>,否則返回字串本身。 * <pre> * StringUtil.defaultIfBlank(null) = "" * StringUtil.defaultIfBlank("") = "" * StringUtil.defaultIfBlank(" ") = "" * StringUtil.defaultIfBlank("bat") = "bat" * </pre> * * @param str 要轉換的字串 * * @return 字串本身或空字串<code>""</code> */ public static String defaultIfBlank(String str) { return isBlank(str) ? EMPTY_STRING : str; } /** * 如果字串是<code>null</code>或空字串<code>""</code>,則返回指定預設字串,否則返回字串本身。 * <pre> * StringUtil.defaultIfBlank(null, "default") = "default" * StringUtil.defaultIfBlank("", "default") = "default" * StringUtil.defaultIfBlank(" ", "default") = "default" * StringUtil.defaultIfBlank("bat", "default") = "bat" * </pre> * * @param str 要轉換的字串 * @param defaultStr 預設字串 * * @return 字串本身或指定的預設字串 */ public static String defaultIfBlank(String str, String defaultStr) { return isBlank(str) ? defaultStr : str; } /* ============================================================================ */ /* 去空白(或指定字元)的函式。 */ /* */ /* 以下方法用來除去一個字串中的空白或指定字元。 */ /* ============================================================================ */ /** * 除去字串頭尾部的空白,如果字串是<code>null</code>,依然返回<code>null</code>。 * * <p> * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>來判定空白, * 因而可以除去英文字符集之外的其它空白,如中文空格。 * <pre> * StringUtil.trim(null) = null * StringUtil.trim("") = "" * StringUtil.trim(" ") = "" * StringUtil.trim("abc") = "abc" * StringUtil.trim(" abc ") = "abc" * </pre> * </p> * * @param str 要處理的字串 * * @return 除去空白的字串,如果原字串為<code>null</code>,則返回<code>null</code> */ public static String trim(String str) { return trim(str, null, 0); } /** * 除去字串頭尾部的指定字元,如果字串是<code>null</code>,依然返回<code>null</code>。 * <pre> * StringUtil.trim(null, *) = null * StringUtil.trim("", *) = "" * StringUtil.trim("abc", null) = "abc" * StringUtil.trim(" abc", null) = "abc" * StringUtil.trim("abc ", null) = "abc" * StringUtil.trim(" abc ", null) = "abc" * StringUtil.trim(" abcyx", "xyz") = " abc" * </pre> * * @param str 要處理的字串 * @param stripChars 要除去的字元,如果為<code>null</code>表示除去空白字元 * * @return 除去指定字元後的的字串,如果原字串為<code>null</code>,則返回<code>null</code> */ public static String trim(String str, String stripChars) { return trim(str, stripChars, 0); } /** * 除去字串頭部的空白,如果字串是<code>null</code>,則返回<code>null</code>。 * * <p> * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>來判定空白, * 因而可以除去英文字符集之外的其它空白,如中文空格。 * <pre> * StringUtil.trimStart(null) = null * StringUtil.trimStart("") = "" * StringUtil.trimStart("abc") = "abc" * StringUtil.trimStart(" abc") = "abc" * StringUtil.trimStart("abc ") = "abc " * StringUtil.trimStart(" abc ") = "abc " * </pre> * </p> * * @param str 要處理的字串 * * @return 除去空白的字串,如果原字串為<code>null</code>或結果字串為<code>""</code>,則返回<code>null</code> */ public static String trimStart(String str) { return trim(str, null, -1); } /** * 除去字串頭部的指定字元,如果字串是<code>null</code>,依然返回<code>null</code>。 * <pre> * StringUtil.trimStart(null, *) = null * StringUtil.trimStart("", *) = "" * StringUtil.trimStart("abc", "") = "abc" * StringUtil.trimStart("abc", null) = "abc" * StringUtil.trimStart(" abc", null) = "abc" * StringUtil.trimStart("abc ", null) = "abc " * StringUtil.trimStart(" abc ", null) = "abc " * StringUtil.trimStart("yxabc ", "xyz") = "abc " * </pre> * * @param str 要處理的字串 * @param stripChars 要除去的字元,如果為<code>null</code>表示除去空白字元 * * @return 除去指定字元後的的字串,如果原字串為<code>null</code>,則返回<code>null</code> */ public static String trimStart(String str, String stripChars) { return trim(str, stripChars, -1); } /** * 除去字串尾部的空白,如果字串是<code>null</code>,則返回<code>null</code>。 * * <p> * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>來判定空白, * 因而可以除去英文字符集之外的其它空白,如中文空格。 * <pre> * StringUtil.trimEnd(null) = null * StringUtil.trimEnd("") = "" * StringUtil.trimEnd("abc") = "abc" * StringUtil.trimEnd(" abc") = " abc" * StringUtil.trimEnd("abc ") = "abc" * StringUtil.trimEnd(" abc ") = " abc" * </pre> * </p> * * @param str 要處理的字串 * * @return 除去空白的字串,如果原字串為<code>null</code>或結果字串為<code>""</code>,則返回<code>null</code> */ public static String trimEnd(String str) { return trim(str, null, 1); } /** * 除去字串尾部的指定字元,如果字串是<code>null</code>,依然返回<code>null</code>。 * <pre> * StringUtil.trimEnd(null, *) = null * StringUtil.trimEnd("", *) = "" * StringUtil.trimEnd("abc", "") = "abc" * StringUtil.trimEnd("abc", null) = "abc" * StringUtil.trimEnd(" abc", null) = " abc" * StringUtil.trimEnd("abc ", null) = "abc" * StringUtil.trimEnd(" abc ", null) = " abc" * StringUtil.trimEnd(" abcyx", "xyz") = " abc" * </pre> * * @param str 要處理的字串 * @param stripChars 要除去的字元,如果為<code>null</code>表示除去空白字元 * * @return 除去指定字元後的的字串,如果原字串為<code>null</code>,則返回<code>null</code> */ public static String trimEnd(String str, String stripChars) { return trim(str, stripChars, 1); } /** * 除去字串頭尾部的空白,如果結果字串是空字串<code>""</code>,則返回<code>null</code>。 * * <p> * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>來判定空白, * 因而可以除去英文字符集之外的其它空白,如中文空格。 * <pre> * StringUtil.trimToNull(null) = null * StringUtil.trimToNull("") = null * StringUtil.trimToNull(" ") = null * StringUtil.trimToNull("abc") = "abc" * StringUtil.trimToNull(" abc ") = "abc" * </pre> * </p> * * @param str 要處理的字串 * * @return 除去空白的字串,如果原字串為<code>null</code>或結果字串為<code>""</code>,則返回<code>null</code> */ public static String trimToNull(String str) { return trimToNull(str, null); } /** * 除去字串頭尾部的空白,如果結果字串是空字串<code>""</code>,則返回<code>null</code>。 * * <p> * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>來判定空白, * 因而可以除去英文字符集之外的其它空白,如中文空格。 * <pre> * StringUtil.trim(null, *) = null * StringUtil.trim("", *) = null * StringUtil.trim("abc", null) = "abc" * StringUtil.trim(" abc", null) = "abc" * StringUtil.trim("abc ", null) = "abc" * StringUtil.trim(" abc ", null) = "abc" * StringUtil.trim(" abcyx", "xyz") = " abc" * </pre> * </p> * * @param str 要處理的字串 * @param stripChars 要除去的字元,如果為<code>null</code>表示除去空白字元 * * @return 除去空白的字串,如果原字串為<code>null</code>或結果字串為<code>""</code>,則返回<code>null</code> */ public static String trimToNull(String str, String stripChars) { String result = trim(str, stripChars); if ((result == null) || (result.length() == 0)) { return null; } return result; } /** * 除去字串頭尾部的空白,如果字串是<code>null</code>,則返回空字串<code>""</code>。 * * <p> * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>來判定空白, * 因而可以除去英文字符集之外的其它空白,如中文空格。 * <pre> * StringUtil.trimToEmpty(null) = "" * StringUtil.trimToEmpty("") = "" * StringUtil.trimToEmpty(" ") = "" * StringUtil.trimToEmpty("abc") = "abc" * StringUtil.trimToEmpty(" abc ") = "abc" * </pre> * </p> * * @param str 要處理的字串 * * @return 除去空白的字串,如果原字串為<code>null</code>或結果字串為<code>""</code>,則返回<code>null</code> */ public static String trimToEmpty(String str) { return trimToEmpty(str, null); } /** * 除去字串頭尾部的空白,如果字串是<code>null</code>,則返回空字串<code>""</code>。 * * <p> * 注意,和<code>String.trim</code>不同,此方法使用<code>Character.isWhitespace</code>來判定空白, * 因而可以除去英文字符集之外的其它空白,如中文空格。 * <pre> * StringUtil.trim(null, *) = "" * StringUtil.trim("", *) = "" * StringUtil.trim("abc", null) = "abc" * StringUtil.trim(" abc", null) = "abc" * StringUtil.trim("abc ", null) = "abc" * StringUtil.trim(" abc ", null) = "abc" * StringUtil.trim(" abcyx", "xyz") = " abc" * </pre> * </p> * * @param str 要處理的字串 * * @return 除去空白的字串,如果原字串為<code>null</code>或結果字串為<code>""</code>,則返回<code>null</code> */ public static String trimToEmpty(String str, String stripChars) { String result = trim(str, stripChars); if (result == null) { return EMPTY_STRING; } return result; } /** * 除去字串頭尾部的指定字元,如果字串是<code>null</code>,依然返回<code>null</code>。 * <pre> * StringUtil.trim(null, *) = null * StringUtil.trim("", *) = "" * StringUtil.trim("abc", null) = "abc" * StringUtil.trim(" abc", null) = "abc" * StringUtil.trim("abc ", null) = "abc" * StringUtil.trim(" abc ", null) = "abc" * StringUtil.trim(" abcyx", "xyz") = " abc" * </pre> * * @param str 要處理的字串 * @param stripChars 要除去的字元,如果為<code>null</code>表示除去空白字元 * @param mode <code>-1</code>表示trimStart,<code>0</code>表示trim全部,<code>1</code>表示trimEnd * * @return 除去指定字元後的的字串,如果原字串為<code>null</code>,則返回<code>null</code> */ private static String trim(String str, String stripChars, int mode) { if (str == null) { return null; } int length = str.length(); int start = 0; int end = length; // 掃描字串頭部 if (mode <= 0) { if (stripChars == null) { while ((start < end) && (Character.isWhitespace(str.charAt(start)))) { start++; } } else if (stripChars.length() == 0) { return str; } else { while ((start < end) && (stripChars.indexOf(str.charAt(start)) != -1)) { start++; } } } // 掃描字串尾部 if (mode >= 0) { if (stripChars == null) { while ((start < end) && (Character.isWhitespace(str.charAt(end - 1)))) { end--; } } else if (stripChars.length() == 0) { return str; } else { while ((start < end) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) { end--; } } } if ((start > 0) || (end < length)) { return str.substring(start, end); } return str; } /* ============================================================================ */ /* 比較函式。 */ /* */ /* 以下方法用來比較兩個字串是否相同。 */ /* ============================================================================ */ /** * 比較兩個字串(大小寫敏感)。 * <pre> * StringUtil.equals(null, null) = true * StringUtil.equals(null, "abc") = false * StringUtil.equals("abc", null) = false * StringUtil.equals("abc", "abc") = true * StringUtil.equals("abc", "ABC") = false * </pre> * * @param str1 要比較的字串1 * @param str2 要比較的字串2 * * @return 如果兩個字串相同,或者都是<code>null</code>,則返回<code>true</code> */ public static boolean equals(String str1, String str2) { if (str1 == null) { return str2 == null; } return str1.equals(str2); } /** * 比較兩個字串(大小寫不敏感)。 * <pre> * StringUtil.equalsIgnoreCase(null, null) = true * StringUtil.equalsIgnoreCase(null, "abc") = false * StringUtil.equalsIgnoreCase("abc", null) = false * StringUtil.equalsIgnoreCase("abc", "abc") = true * StringUtil.equalsIgnoreCase("abc", "ABC") = true * </pre> * * @param str1 要比較的字串1 * @param str2 要比較的字串2 * * @return 如果兩個字串相同,或者都是<code>null</code>,則返回<code>true</code> */ public static boolean equalsIgnoreCase(String str1, String str2) { if (str1 == null) { return str2 == null; } return str1.equalsIgnoreCase(str2); } /* ============================================================================ */ /* 字串型別判定函式。 */ /* */ /* 判定字串的型別是否為:字母、數字、空白等 */ /* ============================================================================ */ /** * 判斷字串是否只包含unicode字母。 * * <p> * <code>null</code>將返回<code>false</code>,空字串<code>""</code>將返回<code>true</code>。 * </p> * <pre> * StringUtil.isAlpha(null) = false * StringUtil.isAlpha("") = true * StringUtil.isAlpha(" ") = false * StringUtil.isAlpha("abc") = true * StringUtil.isAlpha("ab2c") = false * StringUtil.isAlpha("ab-c") = false * </pre> * * @param str 要檢查的字串 * * @return 如果字串非<code>null</code>並且全由unicode字母組成,則返回<code>true</code> */ public static boolean isAlpha(String str) { if (str == null) { return false; } int length = str.length(); for (int i = 0; i < length; i++) { if (!Character.isLetter(str.charAt(i))) { return false; } } return true; } /** * 判斷字串是否只包含unicode字母和空格<code>' '</code>。 * * <p> * <code>null</code>將返回<code>false</code>,空字串<code>""</code>將返回<code>true</code>。 * </p> * <pre> * StringUtil.isAlphaSpace(null) = false * StringUtil.isAlphaSpace("") = true * StringUtil.isAlphaSpace(" ") = true * StringUtil.isAlphaSpace("abc") = true * StringUtil.isAlphaSpace("ab c") = true * StringUtil.isAlphaSpace("ab2c") = false * StringUtil.isAlphaSpace("ab-c") = false * </pre> * * @param str 要檢查的字串 * * @return 如果字串非<code>null</code>並且全由unicode字母和空格組成,則返回<code>true</code> */ public static boolean isAlphaSpace(String str) { if (str == null) { return false; } int length = str.length(); for (int i = 0; i < length; i++) { if (!Character.isLetter(str.charAt(i)) && (str.charAt(i) != ' ')) { return false; } } return true; } /** * 判斷字串是否只包含unicode字母和數字。 * * <p> * <code>null</code>將返回<code>false</code>,空字串<code>""</code>將返回<code>true</code>。 * </p> * <pre> * StringUtil.isAlphanumeric(null) = false * StringUtil.isAlphanumeric("") = true * StringUtil.isAlphanumeric(" ") = false * StringUtil.isAlphanumeric("abc") = true * StringUtil.isAlphanumeric("ab c") = false * StringUtil.isAlphanumeric("ab2c") = true * StringUtil.isAlphanumeric("ab-c") = false * </pre> * * @param str 要檢查的字串 * * @return 如果字串非<code>null</code>並且全由unicode字母數字組成,則返回<code>true</code> */ public static boolean isAlphanumeric(String str) { if (str == null) { return false; } int length = str.length(); for (int i = 0; i < length; i++) { if (!Character.isLetterOrDigit(str.charAt(i))) { return false; } } return true; } /** * 判斷字串是否只包含unicode字母數字和空格<code>' '</code>。 * * <p> * <code>null</code>將返回<code>false</code>,空字串<code>""</code>將返回<code>true</code>。 * </p> * <pre> * StringUtil.isAlphanumericSpace(null) = false * StringUtil.isAlphanumericSpace("") = true * StringUtil.isAlphanumericSpace(" ") = true * StringUtil.isAlphanumericSpace("abc") = true * StringUtil.isAlphanumericSpace("ab c") = true * StringUtil.isAlphanumericSpace("ab2c") = true * StringUtil.isAlphanumericSpace("ab-c") = false * </pre> * * @param str 要檢查的字串 * * @return 如果字串非<code>null</code>並且全由unicode字母數字和空格組成,則返回<code>true</code> */ public static boolean isAlphanumericSpace(String str) { if (str == null) { return false; } int length = str.length(); for (int i = 0; i < length; i++) { if (!Character.isLetterOrDigit(str.charAt(i)) && (str.charAt(i) != ' ')) { return false; } } return true; } /** * 判斷字串是否只包含unicode數字。 * * <p> * <code>null</code>將返回<code>false</code>,空字串<code>""</code>將返回<code>true</code>。 * </p> * <pre> * StringUtil.isNumeric(null) = false * StringUtil.isNumeric("") = true * StringUtil.isNumeric(" ") = false * StringUtil.isNumeric("123") = true * StringUtil.isNumeric("12 3") = false * StringUtil.isNumeric("ab2c") = false * StringUtil.isNumeric("12-3") = false * StringUtil.isNumeric("12.3") = false * </pre> * * @param str 要檢查的字串 * * @return 如果字串非<code>null</code>並且全由unicode數字組成,則返回<code>true</code> */ public static boolean isNumeric(String str) { if (str == null) { return false; } int length = str.length(); for (int i = 0; i < length; i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } /** * 判斷字串是否只包含unicode數字,包括小數。 * * <p> * <code>null</code>將返回<code>false</code>,空字串<code>""</code>將返回<code>true</code>。 * </p> * <pre> * StringUtil.isNumeric(null) = false * StringUtil.isNumeric("") = false * StringUtil.isNumeric(" ") = false * StringUtil.isNumeric("123") = true * StringUtil.isNumeric("12 3") = false * StringUtil.isNumeric("ab2c") = false * StringUtil.isNumeric("12-3") = false * StringUtil.isNumeric("12.3") = true * </pre> * * @param str 要檢查的字串 * * @return 如果字串非<code>null</code>並且全由unicode數字組成,則返回<code>true</code> */ public static boolean isNumber(String str) { if (isBlank(str)) { return false; } int index = str.indexOf("."); if (index < 0) { return isNumeric(str); } else { String num1 = str.substring(0, index); String num2 = str.substring(index + 1); return isNumeric(num1) && isNumeric(num2); } } /** * 判斷字串是否只包含unicode數字和空格<code>' '</code>。 * * <p> * <code>null</code>將返回<code>false</code>,空字串<code>""</code>將返回<code>true</code>。 * </p> * <pre> * StringUtil.isNumericSpace(null) = false * StringUtil.isNumericSpace("") = true * StringUtil.isNumericSpace(" ") = true * StringUtil.isNumericSpace("123") = true * StringUtil.isNumericSpace("12 3") = true * StringUtil.isNumericSpace("ab2c") = false * StringUtil.isNumericSpace("12-3") = false * StringUtil.isNumericSpace("12.3") = false * </pre> * * @param str 要檢查的字串 * * @return 如果字串非<code>null</code>並且全由unicode數字和空格組成,則返回<code>true</code> */ public static boolean isNumericSpace(String str) { if (str == null) { return false; } int length = str.length(); for (int i = 0; i < length; i++) { if (!Character.isDigit(str.charAt(i)) && (str.charAt(i) != ' ')) { return false; } } return true; } /** * 判斷字串是否只包含unicode空白。 * * <p> * <code>null</code>將返回<code>false</code>,空字串<code>""</code>將返回<code>true</code>。 * </p> * <pre> * StringUtil.isWhitespace(null) = false * StringUtil.isWhitespace("") = true * StringUtil.isWhitespace(" ") = true * StringUtil.isWhitespace("abc") = false * StringUtil.isWhitespace("ab2c") = false * StringUtil.isWhitespace("ab-c") = false * </pre> * * @param str 要檢查的字串 * * @return 如果字串非<code>null</code>並且全由unicode空白組成,則返回<code>true</code> */ public static boolean isWhitespace(String str) { if (str == null) { return false; } int length = str.length(); for (int i = 0; i < length; i++) { if (!Character.isWhitespace(str.charAt(i))) { return false