【发布时间】:2017-05-17 03:05:35
【问题描述】:
我有一个函数可以从文件中读取停用词并将其保存在 HashSet 中。
HashSet<String> hset = readFile();
这是我的字符串
String words = "the plan crash is invisible";
我正在尝试从字符串中删除所有停用词,但它无法正常工作
我得到的输出:计划崩溃不可见
我想要的输出 => 计划崩溃不可见
代码:
HashSet<String> hset = readFile();
String words = "the plan crash is invisible";
String s = words.toLowerCase();
String[] split = s.split(" ");
for(String str: split){
if (hset.contains(str)) {
s = s.replace(str, "");
} else {
}
}
System.out.println("\n" + "\n" + s);
【问题讨论】:
-
尝试使用 equals() 或 equalsIgnoreCase()
-
而且您不应该使用
replace,因为当您尝试删除整个单词“is”时,它会删除不可见的“is”。或者至少你应该改变你使用它的方式,以确保它只删除整个单词。
标签: java replace stop-words