gpt4 book ai didi

java - 如何使用replaceAll方法将双引号替换为"

转载 作者:行者123 更新时间:2023-12-02 04:06:44 26 4
gpt4 key购买 nike

我之前见过其他人问过类似的问题,并按照这些人提供的说明进行操作,但我仍然无法让我的代码正常运行。

try {

FileReader fr_p = new FileReader("p.txt");
BufferedReader in_p = new BufferedReader(fr_p);

String line = in_p.readLine();

for (;;) {

line = line.replaceAll("&","&");
line = line.replaceAll("<","&lt;");
line = line.replaceAll(">","&gt;");
line = line.replaceAll("\"","&quot;");

people.add(line);
line = in_p.readLine();
if (line == null) break;

}

in_p.close();
} catch (FileNotFoundException e) {

System.out.println("File p.txt not found.");
System.exit(0);

} catch (IOException e) {

System.out.println("Error reading from file.");
System.exit(0);

}

这是我编写的代码,用于尝试将每个名称放在文本文件的单独行中,并将其放入 ArrayList 中,并用其 XML 实体替换特殊字符。然后我稍后将其写入 HTML 文件。

我编写的代码对于前三个字符来说效果很好,但是当它到达尝试将任何双引号更改为 " 的行时,它不会更改它们,并且最终给了我 而不是双引号。我不确定我还应该对代码进行哪些更改才能使其正常工作。

最佳答案

当我运行时

String line = "This is a string with \" and \" in it";
line = line.replaceAll("\"","&quot;");
System.out.println(line);

我明白了

This is a string with &quot; and &quot; in it

注意:引号有很多种,但只有一个 " 字符。如果有不同的引号,则不会匹配。

https://en.wikipedia.org/wiki/Quotation_mark

关于java - 如何使用replaceAll方法将双引号替换为",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34214364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com