简体   繁体   中英

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...

edit Basically the issue is that nothing changes, I end up with {"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. 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.

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.

list.removeAll(wordlist)

It will remove all elements from list that exists in 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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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