简体   繁体   English

转义字符串中的双引号

[英]Escape double quotes in a string

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# function 或其他方法来转义双引号,以便不需要更改字符串?

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# 该字符是字符串的一部分而不是字符串终止符的一种方式。

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 the same that is printed with:这与打印的完全相同:

string str = @"He said to me, ""Hello World"". How are you?";

Here is a DEMO .这是一个DEMO

" is still part of your string. "仍然是你的字符串的一部分。

You can check Jon Skeet's Strings in C# and .NET article for more information.您可以在 C# 和 .NET 文章中查看 Jon Skeet 的字符串以获取更多信息。

In C# you can use the backslash to put special characters to your string.在 C# 中,您可以使用反斜杠将特殊字符放入字符串。 For example, to put " , you need to write \" .例如,要放置" ,您需要编写\" There are a lot of characters that you write using the backslash:您使用反斜杠编写了很多字符:

Backslash with other characters反斜杠与其他字符

  \0 nul 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

Any character substitution by numbers:用数字替换任何字符:

  \xh to \xhhhh, or \uhhhh - Unicode character in hexadecimal notation (\x has variable digits, \u has 4 digits)
  \Uhhhhhhhh - Unicode surrogate pair (8 hex digits, 2 characters)

Another thing worth mentioning from C# 6 is interpolated strings can be used along with @ . C# 6 中值得一提的另一件事是插值字符串可以与@一起使用。

Example:例子:

string helloWorld = @"""Hello World""";
string test = $"He said to me, {helloWorld}. How are you?";

Or或者

string helloWorld = "Hello World";
string test = $@"He said to me, ""{helloWorld}"". How are you?";

Check running code here !在此处检查运行代码!

View the reference to interpolation here !此处查看对插值的参考!

You're misunderstanding escaping.你误解了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?你的字符串的实际值仍然是He said to me, "Hello World". How are you? He said to me, "Hello World". How are you? , as you'll see if you print it at runtime. ,你会看到如果你在运行时打印它。

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"";你不能输入string foo = "Foo"bar""; , because that'll invoke a compile error. ,因为这会引发编译错误。 As for the adding part, in string size terms that is not true:至于添加部分,在字符串大小方面是不正确的:

@"""".Length == 1

"\"".Length == 1

2022 UPDATE: Previously the answer would have been "no". 2022 年更新:以前的答案是“不”。 However, C#11 introduces a new feature called "raw string literals."但是,C#11 引入了一个称为“原始字符串文字”的新功能。 To quote the Microsoft documentation:引用 Microsoft 文档:

Beginning with C# 11, you can use raw string literals to more easily create strings that are multi-line, or use any characters requiring escape sequences.从 C# 11 开始,您可以使用原始字符串文字更轻松地创建多行字符串,或使用任何需要转义序列的字符。 Raw string literals remove the need to ever use escape sequences.原始字符串文字消除了使用转义序列的需要。 You can write the string, including whitespace formatting, how you want it to appear in output."您可以编写字符串,包括空格格式,以及您希望它如何出现在 output 中。”

SOURCE: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#raw-string-literals来源: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#raw-string-literals

EXAMPLE: So using the original example, you could do this (note that raw string literals always begin with three or more quotation marks):示例:因此使用原始示例,您可以这样做(请注意,原始字符串文字总是以三个或更多引号开头):

string testSingleLine = """He said to me, "Hello World". How are you?""";
string testMultiLine = """
He said to me, "Hello World". How are you?
""";

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#函数或其他方法可以转义双引号,从而无需更改字符串?

In C#, there are at least four ways to embed a quote within a string:在 C# 中,至少有四种方法可以在字符串中嵌入引号:

  1. Escape quote with a backslash带反斜杠的转义引号
  2. Precede string with @ and use double quotes在字符串前面加上@并使用双引号
  3. Use the corresponding ASCII character使用对应的 ASCII 字符
  4. Use the hexadecimal Unicode character使用十六进制 Unicode 字符

Please refer this document for detailed explanation.详细说明请参考本文档

In C# 11.0 preview you can use raw string literals .C# 11.0预览版中,您可以使用原始字符串文字

Raw string literals are a new format for string literals.原始字符串文字是字符串文字的一种新格式。 Raw string literals can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences.原始字符串文字可以包含任意文本,包括空格、换行符、嵌入引号和其他特殊字符,而无需转义序列。 A raw string literal starts with at least three double-quote (""") characters. It ends with the same number of double-quote characters. Typically, a raw string literal uses three double quotes on a single line to start the string, and three double quotes on a separate line to end the string.原始字符串文字至少以三个双引号 (""") 字符开头。它以相同数量的双引号字符结尾。通常,原始字符串文字在一行中使用三个双引号来开始字符串,和三个双引号在单独的行上以结束字符串。

string test = """He said to me, "Hello World" . How are you?""";

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

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