删除标点符号在Java中不能使用字符串替换

Hea*_*r T 1 java string arraylist replaceall

所以,我要做的是编译一个单词列表,没有8个单独的字典单词列表中的重复.一些字典中有标点符号来分隔单词.以下是我所涉及的标点符号删除.我已经尝试了几种不同的解决方案,我发现堆栈溢出有关正则表达式,以及我在代码中留下的那个.出于某种原因,他们都没有从源词典中删除标点符号.有人可以告诉我它是什么我在这里做错了,可能还有如何修复它?我很茫然,有一个同事检查它,他说这应该也可以.

int i = 1;
boolean checker = true;
Scanner inputWords;
PrintWriter writer = new PrintWriter(
        "/home/htarbox/Desktop/fullDictionary.txt");
String comparison, punctReplacer;

ArrayList<String> compilation = new ArrayList<String>();



while (i <9)
{
inputWords = new Scanner(new File("/home/htarbox/Desktop/"+i+".txt"));
    while(inputWords.hasNext())
    {
        punctReplacer = inputWords.next();
        punctReplacer.replaceAll("[;.:\"()!?\\t\\n]", "");
        punctReplacer.replaceAll(",", "");
        punctReplacer.replaceAll("\u201C", "");
        punctReplacer.replaceAll("\u201D", "");
        punctReplacer.replaceAll("’", "'");

        System.out.println(punctReplacer);
        compilation.add(punctReplacer);
    }
    }
inputWords.close();
}
i = 0;
Run Code Online (Sandbox Code Playgroud)

Bri*_*new 5

这条线

punctReplacer.replaceAll(",", "");
Run Code Online (Sandbox Code Playgroud)

String用你的替换品返回一个新的(你忽略了).它不会修改现有的String.因此,您需要:

punctReplacer = punctReplacer.replaceAll(",", "");
Run Code Online (Sandbox Code Playgroud)

Strings是不可改变的.一旦创建,你就无法改变它们,任何String操作方法都会给你一个新的String