繁体   English   中英

转义引号

[英]Escaping quotes

我正在尝试将字符串保存到数据库,我需要将引号包含在字符串中:

String password = "\"foo\"";

我期望数据库中的值为"foo" (包括引号)。但是,也存储了斜杠。 斜线是否应该只是转义字符而在编译时不保存?

但是,也存储斜杠。

不在您发布的字符串中,不是。 您发布的字符串中包含五个字符:

"
f
o
o
"

可能是因为您用来查看的内容都显示了转义的引号,因此很明显它们并没有界定字符串。

您可以这样证明:

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]: "

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM