如何使用 split() 从 Java 中的句子中删除所有分隔符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31349379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use split() to remove all delimiters from a sentence in Java?
提问by Jace YJ
String text = "Good morning. Have a good class. " +
"Have a good visit. Have fun!";
String[] words = text.split("[ \n\t\r.,;:!?(){");
This splitmethod is provided in text book, meant to remove all the delimiters in the sentence as well as white space character but clearly it is not working and throws a regex exception to my disappointment....I am wondering what could we do here to make it work? The requirement is after the splitmethod, everything in the `String[] words are either just English words without any delimiters attaching to it or whitespace character! Thanks a lot!
这种split方法在教科书中提供,旨在删除句子中的所有分隔符以及空格字符,但显然它不起作用并且抛出了一个正则表达式异常让我感到失望......我想知道我们可以在这里做什么让它起作用?要求是在split方法之后,`String[] 单词中的所有内容要么只是没有附加任何分隔符的英文单词或空格字符!非常感谢!
回答by anubhava
You are missing closing ]in your character class:
您]在字符类中缺少关闭:
String[] words = text.split("[ \n\t\r.,;:!?(){]");
btw you can just do (and it is better option):
顺便说一句,您可以这样做(这是更好的选择):
String[] words = text.split("\W+");
to split on any non-word character.
拆分任何非单词字符。
回答by Sekkuar
String.split()is NOT for removing characters. It is used to divide the Stringinto smaller substrings.
String.split()不是用于删除字符。它用于将String分成更小的子串。
Example:
例子:
String s = "This is a string!";
String[] tokens = s.split(" ");
Split will have used the String " " (one space character) as a delimiter to, well, split the string. As a result, the array tokenswill look something like
拆分将使用字符串“”(一个空格字符)作为分隔符来拆分字符串。结果,数组tokens看起来像
{"This", "is", "a", "string!"}
If you want to remove characters, try taking a look at String.replaceAll()
如果要删除字符,请尝试查看String.replaceAll()

