您的位置:首页 > 移动开发 > Android开发

android Character.isWhitespace 判断是否是空白char及提供判断空白字符串

2017-10-18 16:21 501 查看
首先看一下这个文档:http://www.yiibai.com/javalang/character_iswhitespace_codepoint.html  这里其实解释的比较详细了。这有个工具类可以判断字符串的:来自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:* <ul>* <li> 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'}).* <li> It is {@code '\u005Ct'}, U+0009 HORIZONTAL TABULATION.* <li> It is {@code '\u005Cn'}, U+000A LINE FEED.* <li> It is {@code '\u005Cu000B'}, U+000B VERTICAL TABULATION.* <li> It is {@code '\u005Cf'}, U+000C FORM FEED.* <li> It is {@code '\u005Cr'}, U+000D CARRIAGE RETURN.* <li> It is {@code '\u005Cu001C'}, U+001C FILE SEPARATOR.* <li> It is {@code '\u005Cu001D'}, U+001D GROUP SEPARATOR.* <li> It is {@code '\u005Cu001E'}, U+001E RECORD SEPARATOR.* <li> It is {@code '\u005Cu001F'}, U+001F UNIT SEPARATOR.* </ul>** <p><b>Note:</b> This method cannot handle <a* href="#supplementary"> supplementary characters</a>. 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:* <ul>* <li> 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'}).* <li> It is {@code '\u005Ct'}, U+0009 HORIZONTAL TABULATION.* <li> It is {@code '\u005Cn'}, U+000A LINE FEED.* <li> It is {@code '\u005Cu000B'}, U+000B VERTICAL TABULATION.* <li> It is {@code '\u005Cf'}, U+000C FORM FEED.* <li> It is {@code '\u005Cr'}, U+000D CARRIAGE RETURN.* <li> It is {@code '\u005Cu001C'}, U+001C FILE SEPARATOR.* <li> It is {@code '\u005Cu001D'}, U+001D GROUP SEPARATOR.* <li> It is {@code '\u005Cu001E'}, U+001E RECORD SEPARATOR.* <li> It is {@code '\u005Cu001F'}, U+001F UNIT SEPARATOR.* </ul>* <p>** @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了。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐