提问者:小点点

在字符串中转义双引号


双引号可以这样转义:

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

但是这涉及到将字符"添加到字符串中。是否有C#函数或其他方法来转义双引号,以便不需要更改字符串?


共3个答案

匿名用户

没有。

要么使用逐字字符串文字,要么使用反斜杠转义"

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

字符串在这两种情况下都没有改变-其中只有一个转义的。这只是告诉C#字符是字符串的一部分,而不是字符串终止符。

匿名用户

您可以使用任意一种方式的反斜杠:

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

它打印:

He said to me, "Hello World". How are you?

这和打印的完全一样:

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

这里是一个DEMO

"仍然是字符串的一部分。

你可以在C#和.NET文章获取更多信息。

匿名用户

在C#中,可以使用反斜杠在字符串中添加特殊字符。例如,要放置,您需要编写\”。您可以使用反斜杠书写许多字符:

与其他字符的反斜杠

  \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

任何数字字符替换:

  \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)