Can I convert a C# string value to an escaped string literal

后端 未结 16 1007
时光取名叫无心
时光取名叫无心 2020-11-22 07:51

In C#, can I convert a string value to a string literal, the way I would see it in code? I would like to replace tabs, newlines, etc. with their escape sequences.

If

16条回答
  •  一整个雨季
    2020-11-22 08:04

    Interesting question.

    If you can't find a better method, you can always replace.
    In case you're opting for it, you could use this C# Escape Sequence List:

    • \' - single quote, needed for character literals
    • \" - double quote, needed for string literals
    • \ - backslash
    • \0 - Unicode character 0
    • \a - Alert (character 7)
    • \b - Backspace (character 8)
    • \f - Form feed (character 12)
    • \n - New line (character 10)
    • \r - Carriage return (character 13)
    • \t - Horizontal tab (character 9)
    • \v - Vertical quote (character 11)
    • \uxxxx - Unicode escape sequence for character with hex value xxxx
    • \xn[n][n][n] - Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx)
    • \Uxxxxxxxx - Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)

    This list can be found in the C# Frequently Asked Questions What character escape sequences are available?

提交回复
热议问题