刪除某些詞。基本上我有一個字符串。我將字符串中的每個單詞與我在數組中具有的預設數量的單詞相比較。如果字符串中的某個單詞與其中一個預設單詞匹配,則將該單詞從字符串中刪除。從我有一點麻煩搞清楚如何從一個字符串中刪除某些文字字符串
作爲一個例子,我有字符串「是一個測試句」,運行方法後,我應該與單詞的數組{「測試」,「句」}這裏是我迄今...
編輯 基本上問題是,沒有什麼變化,我最終{ 「是」, 「一」, 「測試」, 「句」}
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