这有个工具类可以判断字符串的:来自Blankj的项目
private static boolean isSpace(final String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
然后看一下源码,理解深入一点:
/**
* Determines if the specified character is white space according to Java.
* A character is a Java whitespace character if and only if it satisfies
* one of the following criteria:
*
*
It is a Unicode space character ({@code SPACE_SEPARATOR},* {@code LINE_SEPARATOR}, or {@code PARAGRAPH_SEPARATOR})
* but is not also a non-breaking space ({@code '\u005Cu00A0'},
* {@code '\u005Cu2007'}, {@code '\u005Cu202F'}).
*
It is {@code '\u005Ct'}, U+0009 HORIZONTAL TABULATION.*
It is {@code '\u005Cn'}, U+000A LINE FEED.*
It is {@code '\u005Cu000B'}, U+000B VERTICAL TABULATION.*
It is {@code '\u005Cf'}, U+000C FORM FEED.*
It is {@code '\u005Cr'}, U+000D CARRIAGE RETURN.*
It is {@code '\u005Cu001C'}, U+001C FILE SEPARATOR.*
It is {@code '\u005Cu001D'}, U+001D GROUP SEPARATOR.*
It is {@code '\u005Cu001E'}, U+001E RECORD SEPARATOR.*
It is {@code '\u005Cu001F'}, U+001F UNIT SEPARATOR.*
*
*
Note: This method cannot handle
* href="#supplementary"> supplementary characters
. To support* all Unicode characters, including supplementary characters, use
* the {@link #isWhitespace(int)} method.
*
* @param ch the character to be tested.
* @return {@code true} if the character is a Java whitespace
* character; {@code false} otherwise.
* @see Character#isSpaceChar(char)
* @since 1.1
*/
public static boolean isWhitespace(char ch) {
return isWhitespace((int)ch);
}
/**
* Determines if the specified character (Unicode code point) is
* white space according to Java. A character is a Java
* whitespace character if and only if it satisfies one of the
* following criteria:
*
*
It is a Unicode space character ({@link #SPACE_SEPARATOR},* {@link #LINE_SEPARATOR}, or {@link #PARAGRAPH_SEPARATOR})
* but is not also a non-breaking space ({@code '\u005Cu00A0'},
* {@code '\u005Cu2007'}, {@code '\u005Cu202F'}).
*
It is {@code '\u005Ct'}, U+0009 HORIZONTAL TABULATION.*
It is {@code '\u005Cn'}, U+000A LINE FEED.*
It is {@code '\u005Cu000B'}, U+000B VERTICAL TABULATION.*
It is {@code '\u005Cf'}, U+000C FORM FEED.*
It is {@code '\u005Cr'}, U+000D CARRIAGE RETURN.*
It is {@code '\u005Cu001C'}, U+001C FILE SEPARATOR.*
It is {@code '\u005Cu001D'}, U+001D GROUP SEPARATOR.*
It is {@code '\u005Cu001E'}, U+001E RECORD SEPARATOR.*
It is {@code '\u005Cu001F'}, U+001F UNIT SEPARATOR.*
*
*
* @param codePoint the character (Unicode code point) to be tested.
* @return {@code true} if the character is a Java whitespace
* character; {@code false} otherwise.
* @see Character#isSpaceChar(int)
* @since 1.5
*/
public static boolean isWhitespace(int codePoint) {
// We don't just call into icu4c because of the JNI overhead. Ideally we'd fix that.
// Any ASCII whitespace character?
if ((codePoint >= 0x1c && codePoint <= 0x20) || (codePoint >= 0x09 && codePoint <= 0x0d)) {
return true;
}
if (codePoint < 0x1000) {
return false;
}
// OGHAM SPACE MARK or MONGOLIAN VOWEL SEPARATOR?
if (codePoint == 0x1680 || codePoint == 0x180e) {
return true;
}
if (codePoint < 0x2000) {
return false;
}
// Exclude General Punctuation's non-breaking spaces (which includes FIGURE SPACE).
if (codePoint == 0x2007 || codePoint == 0x202f) {
return false;
}
if (codePoint <= 0xffff) {
// Other whitespace from General Punctuation...
return codePoint <= 0x200a || codePoint == 0x2028 || codePoint == 0x2029 || codePoint == 0x205f ||
codePoint == 0x3000; // ...or CJK Symbols and Punctuation?
}
// Let icu4c worry about non-BMP code points.
return isWhitespaceImpl(codePoint);
}
native static boolean isWhitespaceImpl(int codePoint);
这里面已经是判的
Unicode
了。