【问题标题】:Escape double quotes in a string转义字符串中的双引号
【发布时间】:2013-01-06 23:48:20
【问题描述】:

双引号可以这样转义:

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

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

【问题讨论】:

标签: c# string double-quotes


【解决方案1】:

没有。

要么像你一样使用逐字字符串文字,要么使用反斜杠转义"。

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

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

【讨论】:

    【解决方案2】:

    你误会了逃跑。

    额外的" 字符是字符串文字的一部分;它们被编译器解释为 single "。

    你的字符串的实际值仍然是He said to me, "Hello World". How are you?,如果你在运行时打印它就会看到。

    【讨论】:

      【解决方案3】:

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

      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。

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

      您可以查看 Jon Skeet 的 Strings in C# and .NET 文章了解更多信息。

      【讨论】:

        【解决方案4】:

        请解释您的问题。你说:

        但这涉及在字符串中添加字符 "。

        这是什么问题?您不能输入string foo = "Foo"bar"";,因为这会引发编译错误。至于 adding 部分,在字符串大小方面是不正确的:

        @"""".Length == 1
        
        "\"".Length == 1
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            在 C# 中,至少有四种方法可以在字符串中嵌入引号:

            1. 用反斜杠转义引号
            2. 在字符串前面加上@ 并使用双引号
            3. 使用对应的ASCII字符
            4. 使用十六进制 Unicode 字符

            详细解释请参考this document。

            【讨论】:

            • 如果您在此处进行总结(通过编辑您的答案)会更好 - 因为链接可能随时断开。例如,“使用对应的 ASCII 字符” 究竟是什么意思?它在源代码中究竟是如何编码的?您可以为这四种方式中的每一种提供一个或多个(很棒的)代码示例。
            【解决方案7】:

            在 C# 6 中值得一提的另一件事是插值字符串可以与 @ 一起使用。

            例子:

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

            或者

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

            检查运行代码here!

            查看插值参考here!

            【讨论】:

            • 这适用于哪个 C# 版本?你能提供一些参考吗?请通过editing your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来好像是今天写的)。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-24
            相关资源
            最近更新 更多