繁体   English   中英

如何从字符串中删除一些单词

[英]How to remove some words from a string

我想从字符串中删除某些单词。 我要删除的词是:“a”,“an”,“and”,“the”,“of”和“or”。

我使用以下方法:

 void doNoiseEliminator(Vector<String> input){

        noNoiseLines = new Vector<String>();
        String temp;   

        for(int i = 0; i < input.size(); i++) {

            String regex = "(\\sand\\s)|(\\sa\\s)|(\\sthe\\s)|(\\san\\s)|(\\sof\\s)|(\\sor\\s)";
            temp = input.get(i).replaceAll(regex, " ");
            noNoiseLines.add(temp);            
        }
    }

但这似乎不起作用。 我的程序采用字符串行并循环移动该行。

对于以下输入:

我的名字是约翰
我的名字是SAM
我的名字是或者是原始的

输出是:

  1. 山姆,我的名字是
  2. 是个山姆我的名字
  3. 约翰我的名字
  4. 原来我的名字
  5. 约翰我的名字是
  6. 我的名字是山姆
  7. 我的名字是约翰
  8. 我的名字是生的
  9. 名字是山姆我的
  10. 名字是约翰我的
  11. 名字是生我的
  12. 原来,我的名字是
  13. 山姆,我的名字是

为什么会这样? 我怎么能纠正这个? 请帮我。 谢谢...!!!

说实话,我完全不理解你的问题,但首先尝试简单的方法,没有正则表达式,你的问题可能就在那里。 然后根据需要进行优化。

例如,尝试这样的事情。

void doNoiseEliminator(向量输入){

    noNoiseLines = new Vector<String>();
    String temp;   

    for(int i = 0; i < input.size(); i++) {


        temp = input.get(i).replaceAll(" a ", " ").replaceAll(" an ", " ").replaceAll(" and ", " ").replaceAll(" the ", " ").replaceAll(" of ", " ").replaceAll(" or ", " ");
        noNoiseLines.add(temp);            
    }
}

当然这不应该是最终的解决方案,只是为了检查它是否有效。 工作,你可以去检查/修复正则表达式或任何其他解决方案。

希望它有助于指导解决方案,cya。

嘿这样使用 -

 noNoiseLines = new Vector<String>();
String temp;   

for(int i = 0; i < input.size(); i++) {


    temp = input.get(i).replaceAll(" and|an|a|the|of|or ", " ");
    noNoiseLines.add(temp);            
}

}

先放 一个 如果你面前放它将替换包含有“” 一个字,包括所有的次数,只有保持为

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM