英文:
How do I make it so a String is turned into only words with spaces in between each?
问题
我看到一些其他人之前解决过这个问题,但在转换为字符串时仍然会出现一些小问题,比如
yes, it is true
会变成
yes it is true
我已经尽力解决了这个问题。有人可以帮我解决一下吗?因为我正在尝试计算所有的单词,但它最终将连续的空格也视为一个单词。请帮帮我!
我目前的解决方法如下:
public String cleanToken(String token2){
token2 = token2.replaceAll("'s", " ");
token2 = token2.replaceAll("\\r|\\n", " ");
token2 = token2.replaceAll("\\p{Punct}", " ");
token2 = token2.replaceAll(" ", "");
token2 = token2.replaceAll(" ", "");
token2 = token2.replaceAll("\t", " ");
token2 = token2.toLowerCase();
return token2;
}
提前感谢您的帮助。
英文:
I saw some other people who solved this problem before but there are still some hiccups when it comes to turning it into a String, such as
yes, it is true
turning into
yes it is true
I tried to solve this problem to the best of my ability. Could someone help me with this because I am trying to count all the words and it eventually considers "" (the space between the spaces) as a word. Please help!
What I have so far:
public String cleanToken(String token2){
token2 = token2.replaceAll("'s", " ");
token2 = token2.replaceAll("\\r|\\n", " ");
token2 = token2.replaceAll("\\p{Punct}", " ");
token2 = token2.replaceAll(" ", "");
token2 = token2.replaceAll(" ", "");
token2 = token2.replaceAll("\t", " ");
token2 = token2.toLowerCase();
return token2;
}
Thanks in advance
答案1
得分: 2
\\s
将匹配任何空白字符。所以,类似这样的操作:
return token2
.replaceAll("'s", " ")
.replaceAll("\\p{Punct}", " ")
.replaceAll("\\s+", " ")
.toLowerCase();
英文:
\\s
will match any whitespace. So, something like
return token2
.replaceAll("'s", " ")
.replaceAll("\\p{Punct}", " ")
.replaceAll("\\s+", " ")
.toLowerCase();
答案2
得分: 0
StringUtils.normalizeSpace(s.replaceAll("\\W", ""));
英文:
StringUtils.normalizeSpace(s.replaceAll("\\W", ""));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论