如何将一个字符串转换为只包含以空格分隔的单词?

huangapple go评论132阅读模式
英文:

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", ""));

huangapple
  • 本文由 发表于 2020年5月4日 08:46:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61583532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定