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

后端 未结 16 847
时光取名叫无心
时光取名叫无心 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:18

    Code:

    string someString1 = "\tHello\r\n\tWorld!\r\n";
    string someString2 = @"\tHello\r\n\tWorld!\r\n";
    
    Console.WriteLine(someString1);
    Console.WriteLine(someString2);
    

    Output:

        Hello
        World!
    
    \tHello\r\n\tWorld!\r\n
    

    Is this what you want?

提交回复
热议问题