adding line break

后端 未结 7 1954
有刺的猬
有刺的猬 2020-12-05 14:13

I have a problem with adding line break in a string. I have tried using \"\\r\\n\", and Environment.NewLine also does not work.

FirmNames = \"\";

foreach (v         


        
相关标签:
7条回答
  • 2020-12-05 14:18

    C# 6+

    In addition, since c#6 you can also use a static using statement for System.Environment.

    So instead of Environment.NewLine, you can just write NewLine.

    Concise and much easier on the eye, particularly when there are multiple instances ...

    using static System.Environment;
       
    FirmNames = "";
    foreach (var item in FirmNameList)
    {
        if (FirmNames != "")
        {
           FirmNames += ", " + NewLine;
        }
        FirmNames += item;
    }
    
    0 讨论(0)
  • 2020-12-05 14:30
    string[] abcd = obj.show(); 
    
    Response.Write(string.join("</br>", abcd));
    
    0 讨论(0)
  • 2020-12-05 14:31

    This worked for me:

    foreach (var item in FirmNameList){
        if (FirmNames != "")
        {
            FirmNames += ",\r\n"
        }
    
        FirmNames += item;
    }
    
    0 讨论(0)
  • 2020-12-05 14:34

    Give this a try.

            FirmNames = String.Join(", \n", FirmNameList);
    
    0 讨论(0)
  • 2020-12-05 14:34

    Try using \n when concatenating strings, as in this example:

    var name = "Raihan";
    var ID = "1234";
    Console.WriteLine(name + "\n" + ID);
    
    0 讨论(0)
  • 2020-12-05 14:38

    The correct answer is to use Environment.NewLine, as you've noted. It is environment specific and provides clarity over "\r\n" (but in reality makes no difference).

    foreach (var item in FirmNameList) 
    {
        if (FirmNames != "")
        {
            FirmNames += ", " + Environment.NewLine;
        }
        FirmNames += item; 
    } 
    
    0 讨论(0)
提交回复
热议问题