33

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 (var item in FirmNameList)
{
    if (FirmNames != "")
    {
       FirmNames += ", " + LineBreak;  -- I want a line break here after the comma ","
    }

    FirmNames += item;
}

Can anyone help?

3
  • What do you get when you Console.WriteLine(FirmNames) after the loop?
    – BoltClock
    Jan 21, 2011 at 3:55
  • 9
    What are you outputting the resulting string to? A control? The console? Some controls don't support line breaks or have properties you have to set to allow line breaks.
    – squillman
    Jan 21, 2011 at 4:02
  • You should use a StringBuilder, as they are much more efficient than appending strings together
    – Tim Cooke
    Aug 31, 2015 at 17:43

9 Answers 9

60

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; 
} 
7

Give this a try.

        FirmNames = String.Join(", \n", FirmNameList);
5

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;
   
...

foreach (var item in FirmNameList)
{
    if (FirmNames != "")
    {
       FirmNames += ", " + NewLine;
    }
    FirmNames += item;
}
4

This worked for me:

foreach (var item in FirmNameList){
    if (FirmNames != "")
    {
        FirmNames += ",\r\n"
    }

    FirmNames += item;
}
2

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

var name = "Raihan";
var ID = "1234";
Console.WriteLine(name + "\n" + ID);
1

\n in c3 working correctly

using System; namespace testing2

public class Test { 
    public static void Main(string[] args) {
        Console.WriteLine("Enter your name");
        String s = Console.ReadLine();
        Console.WriteLine("Your name is " + s + "\n" + "Thank You");
    }
}
0

There are many ways to do it, in my case I use several because some don't work on the platform I need, you can try the following:

Environment.NewLine //--> recommendable
\n
\r
\n\r
\r\n
-1

3 WAYS TO INSERT A NEW LINE IN C# 1:

Console.WriteLine("This is a line");
Console.WriteLine();
Console.WriteLine("This is another line");

2:

Console.WriteLine("This is a line.\nThis is another line.");

3:

Console.WriteLine("This is a line." + Environment.NewLine + "This is another line.");
1
-6
string[] abcd = obj.show(); 

Response.Write(string.join("</br>", abcd));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.