简体   繁体   English

从字符串中删除某些单词

[英]Removing certain words from a string

I'm having a bit of trouble figuring out how to remove certain words from a string. 我在弄清楚如何从字符串中删除某些单词时遇到了一些麻烦。 Basically I have a String. 基本上我有一个字符串。 I compare every word in the string to a preset number of words I have in an array. 我将字符串中的每个单词与数组中的预设单词数量进行比较。 If a word in the string matches one of the preset words I remove that word from the string. 如果字符串中的单词与预设单词之一匹配,我将从字符串中删除该单词。

As an example I have the string "is a test sentence", after running the method I should have an array with the words {"test", "sentence"} Here's what I have thus far... 举例来说,我有一个字符串“ is test test”,运行该方法后,我应该有一个包含单词“ {” test”,“ sentence”}的数组。这就是我到目前为止所拥有的...

edit Basically the issue is that nothing changes, I end up with {"is", "a", "test", "sentence"} 编辑基本上问题是什么都没有改变,我最终得到{“ is”,“ a”,“ test”,“ sentence”}

    private void fillerWords(){

    String[] commonWords = {"the","of","to","and","a","in","is","it","you","that","he","was","for","on","are","with","as","i"};
    List <String>wordList = new ArrayList<String>(Arrays.asList(commonWords)); 

    //Split words in sentence up by word, put them into array
    String s = "is a test sentance";
    String[] tArray = s.split(" ");
    List <String>list = new ArrayList<String>(Arrays.asList(tArray ));    

    //take out words
    for(int i=0; i<list.size(); i++){
        //Check to see if a sentence word is a common word, if so remove word
        for(int c=0; c<wordList.size(); c++){
            if(wordList.get(c) == list.get(i)){
                list.remove(i);
            }//end if
        }//end for
    }//end for


    for(int x=0; x<list.size(); x++){
        System.out.printf("%s  %s \n", x, list.get(x));
    }

}

} }

The problem is that you are removing index i from the list and then incrementing i, so you are skipping one every time you remove. 问题是您要从列表中删除索引i,然后再递增i,因此每次删除时都跳过一个索引。 Maybe create another list called output and instead of removing from "list" when you hit a bad word, just add to "output" when you hit a good word. 也许创建另一个称为输出的列表,而不是在您遇到不好的单词时从“列表”中删除,而是在您遇到好的单词时将其添加到“输出”中。

Also, as Failsafe said, you can't use "==" to compare strings, you need to use string1.equals(string2) to compare. 另外,正如故障保护所说,您不能使用“ ==”来比较字符串,而需要使用string1.equals(string2)进行比较。

Also, here's a short way to fix it without changing much: 另外,这是一种无需过多更改即可解决的简短方法:

Change your compare block as such: 这样更改您的比较块:

if(wordList.get(c).equals(list.get(i))){
   list.remove(i);
   i--;
   break;
}

Use removeAll() to remove elements that exists in another collection. 使用removeAll()删除另一个集合中存在的元素。

list.removeAll(wordlist)

It will remove all elements from list that exists in wordlist . 这将删除所有元素list中存在的wordlist

(your code should work too. but it is a shorter way) (您的代码也应该起作用。但这是一种较短的方法)

You cannot compare strings with 您不能将字符串与

if(wordList.get(c) == list.get(i)){
            list.remove(i);
        }//end if

You need to do: 您需要做:

if(wordList.get(c).equals(list.get(i))){
            list.remove(i);
        }//end if
    String regex;
    regex = "\\s*\\bword\\b\\s*";//word must to be removed.
    while(out.contains("word"))
    out = out.replaceAll(regex, "");//out if input String and finnaly is out..

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

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