quarks
quarks

Reputation: 35276

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?

Upvotes: 2

Views: 175

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074108

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

Upvotes: 6

Related Questions