删除某些词。基本上我有一个字符串。我将字符串中的每个单词与我在数组中具有的预设数量的单词相比较。如果字符串中的某个单词与其中一个预设单词匹配,则将该单词从字符串中删除。从我有一点麻烦搞清楚如何从一个字符串中删除某些文字字符串
作为一个例子,我有字符串“是一个测试句”,运行方法后,我应该与单词的数组{“测试”,“句”}这里是我迄今...
编辑 基本上问题是,没有什么变化,我最终{ “是”, “一”, “测试”, “句”}
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));
}
}
}
你说你要的话{“测试”结束了,“句子”},但你究竟是以什么结束? – Mercurybullet 2012-07-27 20:16:26
这是什么问题?粗略地说,它看起来应该起作用。但是,您应该为停用词使用HashSet,因为您可以保存内部循环,所以可以提供更好的运行时性能。 – Jochen 2012-07-27 20:20:39
我最终得到的是{“是”,“一”,“测试”,“句”} ....基本上没有什么happnes – SNV7 2012-07-27 20:22:49