Escape double quotes in a string

后端 未结 6 2094
轮回少年
轮回少年 2020-11-22 10:35

Double quotes can be escaped like this:

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

But this involves adding chara

相关标签:
6条回答
  • 2020-11-22 11:05

    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.

    0 讨论(0)
  • 2020-11-22 11:13

    One solution, is to add support to the csharp language so that "" isn't the only scheme used for strings.

    For another string terminator to the C# language - I'm a fan of backtick in ES6.

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

    But also, the doubling idea in Markdown might be better:

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

    The code does not work at the date of this post. This post is a solution where the visitors to this Q&A jump onto this csharplank ticket for C# and upvote it - https://github.com/dotnet/csharplang/discussions/3917

    0 讨论(0)
  • 2020-11-22 11:17

    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:

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

    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".
    0 讨论(0)
  • 2020-11-22 11:29

    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.

    0 讨论(0)
  • 2020-11-22 11:30

    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.

    " is still part of your string.

    Check out Escape Sequences and String literals from MSDN.

    0 讨论(0)
  • 2020-11-22 11:30

    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 adding part, in string size terms that is not true:

    @"""".Length == "\"".Length == 1
    
    0 讨论(0)
提交回复
热议问题