How to easily display double quotes in strings in C#?

后端 未结 4 1365
野性不改
野性不改 2021-01-12 04:59

If you have a string with a numerous double quotes,

in PHP you can do this:

file.WriteLine(\'

        
相关标签:
4条回答
  • 2021-01-12 05:19

    There are two ways to represent quotes in C# strings:

    file.WriteLine("<controls:FormField Name=\"Strasse\" LabelText=\"Strasse\">");
    file.WriteLine(@"<controls:FormField Name=""Strasse"" LabelText=""Strasse"">");
    
    0 讨论(0)
  • 2021-01-12 05:25

    I would recommend avoiding some bizarre way like this:

    const char doubleQuote = '"';
    
    Console.WriteLine("<controls:FormField Name={0}Strasse{0} LabelText={0}Strasse{0}>", doubleQuote);
    
    0 讨论(0)
  • 2021-01-12 05:27
    "<controls:FormField Name='Strasse' LabelText='Strasse'>".Replace("'", "\"")
    

    Which isn't great, but about the only option.

    Or @"""" insted of \" you can use ""

    0 讨论(0)
  • 2021-01-12 05:27

    I would recommend using a resource file to store the string constants. This increases elegance and code readability. Also, quotes and special characters can be displayed without messing around with too many escape sequences.

    You can create a resource file entry like,

    String Name(Key) => FormFieldControl
    Value => <controls:FormField Name="{0}" LabelText="{1}">
    

    This can be used in code like

    const string fieldName = "Strasse";
    Console.WriteLine(ResourceFile.FormFieldControl, fieldName, fieldName);
    
    0 讨论(0)
提交回复
热议问题