gpt4 book ai didi

java - 替换 (") with (\"的正则表达式)?

转载 作者:行者123 更新时间:2023-11-30 05:52:41 25 4
gpt4 key购买 nike

为什么以下不起作用:

      String test = "hello\"world".replaceAll("\"", "\\\"");
System.out.println(test);

我想做的是用 \" 替换所有出现的 "

所以我想得到输出:

hello\"world

最佳答案

对此,正则表达式有点矫枉过正。

myString.replace("\"", "\\\"")

应该做得很好,并且对于熟悉核心库的人来说更具可读性。

replace方法只是将一个子字符串替换为另一个子字符串。

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

关于java - 替换 (") with (\"的正则表达式)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11417811/

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