sách gpt4 ai đã đi

Java thay thế cho "with\"

In lại 作者:行者123 更新时间:2023-11-29 09:55:37 31 4
mua khóa gpt4 Nike

我正在尝试用\"替换包含 "的字符串,下面是我尝试过的程序

    String s="\"/test /string\"";
s = s.replaceAll("\"", "\\\"");
System.out.println(s);

但我得到与字符串“/test/string”相同的输出。为什么我的替换功能不起作用。如果我这样做

   s = s.replaceAll("\"", "\\\\\"");

然后我得到我想要的输出\"/test/string\"。为什么以前的 dint 有效,即使在代码中我试图用\"替换“

1 Câu trả lời

您正在使用 String.replaceAll ,它以正则表达式作为输入,包括替换。如 Match.replaceAll() Như đã nêu trong:

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string.

你真的只是想做一个不涉及正则表达式的直接替换,所以使用 String.replace相反:

s = s.replace("\"", "\\\"");

关于Java 替换 "with\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12815891/

31 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com