我正在尝试将字符串保存到数据库中,我需要将引号作为字符串的一部分:
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/