java - 转义引号

标签 java

我正在尝试将字符串保存到数据库中,我需要将引号作为字符串的一部分:

String password = "\"foo\"";

我希望数据库中的值是 "foo"(包括引号) 但是斜杠也被存储。斜杠应该只是一个转义字符,编译时不保存吗?

最佳答案

However the slash is also stored.

不在您发布的字符串中。您发布的字符串中有五个字符:

"
f
o
o
"

It may be that whatever you're using to view it is showing the quotes escaped so that it's clear that they're not delimiting the string.

You can prove this like so:

public class ShowString {
    public static final void main(String[] args) {
        String password = "\"foo\"";
        int    index, len;

        for (index = 0, len = password.length(); index < len; ++index) {
            System.out.println("[" + index + "]: " + password.charAt(index));
        }
    }
}

输出:

[0]: "
[1]: f
[2]: o
[3]: o
[4]: "

关于java - 转义引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11259502/

相关文章:

java - 这个电子邮件程序如何工作?

java - 如何在Windows 7中安装旧版本的JDK?

java - JAXB 将多个对象编码到一个文件

java - 显示通话中的拨号盘 - 通话时调用号码 - DTMF

java - 按钮不会相互水平显示。只是相互重叠

java - 我如何将这段代码整理成java中的循环?

java - 用于运行/停止程序的 for 循环

java - 如何清空 JTable?

java - 通过 InjectMocks Spy 注入(inject)对象

java - 如何将现有的 Java 应用程序转换为 SYS V 服务(守护进程)