简体   繁体   English

转义引号

[英]Escaping quotes

I'm trying to save a String to a database, I need the quotes to be part of the string: 我正在尝试将字符串保存到数据库,我需要将引号包含在字符串中:

String password = "\"foo\"";

I am expecting the value in the database to be "foo" (quotes included) However the slash is also stored. 我期望数据库中的值为"foo" (包括引号)。但是,也存储了斜杠。 Should the slash be just a escape character and not be saved when compiled? 斜线是否应该只是转义字符而在编译时不保存?

However the slash is also stored. 但是,也存储斜杠。

Not in the string you've posted it isn't. 不在您发布的字符串中,不是。 The string you've posted has five characters in it: 您发布的字符串中包含五个字符:

"
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));
        }
    }
}

Output: 输出:

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

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

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