How can I create a carriage return in my C# string

前端 未结 5 1459
无人共我
无人共我 2021-01-07 16:49

I have C# code that creates HTML. One particular piece of code creates a long string which is no problem for the browser. However when I look at the code with view > source

相关标签:
5条回答
  • 2021-01-07 17:29

    You can put \r\n in your string.

    0 讨论(0)
  • 2021-01-07 17:35

    Along with Environment.NewLine and the literal \r\n or just \n you may also use a verbatim string in C#. These begin with @ and can have embedded newlines. The only thing to keep in mind is that " needs to be escaped as "". An example:

    string s = @"This is a string
    that contains embedded new lines,
    that will appear when this string is used."
    
    0 讨论(0)
  • 2021-01-07 17:36
    string myHTML = "some words " + Environment.NewLine + "more words");
    
    0 讨论(0)
  • 2021-01-07 17:36
    <br /> works for me
    

    So...

    String body = String.Format(@"New user: 
     <br /> Name: {0}
     <br /> Email: {1}
     <br /> Phone: {2}", Name, Email, Phone);
    

    Produces...

    New user:
    Name: Name
    Email: Email
    Phone: Phone

    0 讨论(0)
  • 2021-01-07 17:39

    myString += Environment.NewLine;

    myString = myString + Environment.NewLine;
    
    0 讨论(0)
提交回复
热议问题