我被要求按Character.IsWhiteSpace = true
所有字符分割字符串。
Character.IsWhiteSpace
是否等同于:
c == " " || c == "\t" || c == "\r" || c == "\n"
修改
如果你能帮助想一个按照这个标准划分文本的简洁方法,我会感到很高兴。
答案 0 :(得分:3)
Here you can read more about it.
从那里采取:
根据确定指定的字符是否为空格 Java的。当且仅当字符是Java空白字符时 满足以下标准之一:
It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
It is '\t', U+0009 HORIZONTAL TABULATION.
It is '\n', U+000A LINE FEED.
It is '\u000B', U+000B VERTICAL TABULATION.
It is '\f', U+000C FORM FEED.
It is '\r', U+000D CARRIAGE RETURN.
It is '\u001C', U+001C FILE SEPARATOR.
It is '\u001D', U+001D GROUP SEPARATOR.
It is '\u001E', U+001E RECORD SEPARATOR.
It is '\u001F', U+001F UNIT SEPARATOR.
答案 1 :(得分:1)
分割这些字符的最简单方法是:
String[] words = input.split("\\p{javaWhitespace}+");
记录在Pattern
javadoc:
\p{javaWhitespace}
相当于java.lang.Character.isWhitespace()
特别是,\\s
上的拆分不等同,因为它不会在\u001C...\u001F
上拆分。