我正在尝试将 String
作为输入,用空格字符将其分割,以便将单词存储在数组中,然后使用这些单词与其他单词进行比较。问题是我希望在分割期间忽略句点,因为与它们进行比较的单词永远不会包含句点。
例如:
String testString = "This. is. a. test. string.";
String[] test = testString.split(" ");
for (int i = 0; i < test.length; i++) {
System.out.println(test[i]);
}
这将打印出:
This.
is.
a.
test.
string.
相反,我希望它打印出来:
This
is
a
test
string
如何忽略分割期间的时间段?
最佳答案
怎么样
String[] test = testString.split("\\.[ ]*");
或
String[] test = testString.split("\\.\\s*");
或超过一个句点(和省略号)
String[] test = testString.split("\\.+\\s*");
关于java - 按空格分割字符串并忽略句点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058695/