C# 转义字符串中的双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14480724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Escape double quotes in string
提问by Mudassir Hasan
Double quotes can be escaped like this:
双引号可以这样转义:
string test = @"He said to me, ""Hello World"". How are you?";
But this involves adding character "
to the string. Is there a C# function or other method to escape double quotes so that no changing in string is required?
但这涉及向"
字符串添加字符。是否有 C# 函数或其他方法来转义双引号,以便不需要更改字符串?
采纳答案by Oded
No.
不。
Either use verbatim string literals as you have, or escape the "
using backslash.
要么使用逐字字符串文字,要么"
使用反斜杠转义。
string test = "He said to me, \"Hello World\" . How are you?";
The string has not changed in either case - there is a single escaped"
in it. This is just a way to tell C# that the character is part of the string and not a string terminator.
在任何一种情况下,字符串都没有改变 - 其中有一个转义"
。这只是告诉 C# 字符是字符串的一部分而不是字符串终止符的一种方式。
回答by SLaks
You're misunderstanding escaping.
你误解了逃避。
The extra "
characters are part of the string literal; they are interpreted by the compiler as a single"
.
额外的"
字符是字符串文字的一部分;它们被编译器解释为单个"
.
The actual value of your string is still He said to me , "Hello World".How are you ?
, as you'll see if you print it at runtime.
你的字符串的实际值仍然是He said to me , "Hello World".How are you ?
,你会看到如果你在运行时打印它。
回答by Soner G?nül
You can use backslash either way;
您可以使用任何一种方式使用反斜杠;
string str = "He said to me, \"Hello World\". How are you?";
It prints;
它打印;
He said to me, "Hello World". How are you?
which is exactly same prints with;
这是完全相同的打印;
string str = @"He said to me, ""Hello World"". How are you?";
Here is a DEMO
.
这是一个DEMO
.
"
is still part of your string.
"
仍然是你的字符串的一部分。
Check out Escape Sequences
and String literals
from MSDN.
签出Escape Sequences
并String literals
从 MSDN。
回答by CodeCaster
Please explain your problem. You say:
请解释您的问题。你说:
But this involves adding character " to the string.
但这涉及向字符串添加字符“。
What problem is that? You can't type string foo = "Foo"bar"";
, because that'll invoke a compile error. As for the addingpart, in string size terms that is not true:
那是什么问题?您不能键入string foo = "Foo"bar"";
,因为这会引发编译错误。至于添加部分,在字符串大小方面是不正确的:
@"""".Length == "\"".Length == 1
回答by Daniel Antokoletz
In C# you can use the backslash to put special characters to your string. For example, to put ", you need to write \". There are a lot of characters that you write using the backslash: Backslash with a number:
在 C# 中,您可以使用反斜杠将特殊字符放入字符串中。例如,要放“,你需要写\”。您可以使用反斜杠编写很多字符: 带有数字的反斜杠:
- \000 null
- \010 backspace
- \011 horizontal tab
- \012 new line
- \015 carriage return
- \032 substitute
- \042 double quote
- \047 single quote
- \134 backslash
- \140 grave accent
- \000 空
- \010 退格
- \011 水平制表符
- \012 新行
- \015 回车
- \032 替代品
- \042 双引号
- \047 单引号
- \134 反斜杠
- \140 重口音
Backslash with othe character
带有其他字符的反斜杠
- \a Bell (alert)
- \b Backspace
- \f Formfeed
- \n New line
- \r Carriage return
- \t Horizontal tab
- \v Vertical tab
- \' Single quotation mark
- \" Double quotation mark
- \ Backslash
- \? Literal question mark
- \ ooo ASCII character in octal notation
- \x hh ASCII character in hexadecimal notation
- \x hhhh Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal. For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese character for one is \x4e00".
- \a 铃(警报)
- \b 退格
- \f 换页
- \n 换行
- \r 回车
- \t 水平制表符
- \v 垂直制表符
- \' 单引号
- \" 双引号
- \ 反斜杠
- \? 字面问号
- \ ooo 八进制表示的 ASCII 字符
- \x hh 十六进制表示的 ASCII 字符
- \x hhhh 如果在宽字符常量或 Unicode 字符串文字中使用此转义序列,则为十六进制表示法的 Unicode 字符。例如,WCHAR f = L'\x4e00' 或 WCHAR b[] = L“一的汉字是\x4e00”。