简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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