我在弄清楚如何从字符串中删除某些单词时遇到了一些麻烦。基本上我有一个字符串。我将字符串中的每个单词与数组中预设的单词数进行比较。如果字符串中的单词与预设单词之一匹配,我会从字符串中删除该单词。
作为一个例子,我有字符串“是一个测试句子”,在运行该方法后,我应该有一个带有单词 {“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));
}
}
}