Copy string to clipboard with inserted line breaks

Hi,

Have a collection of numbers in a listbox. I want to copy these numbersto the clipboard and pasted inside a notepad text file. I loop through the items in the listbox adding them to a string variable separated with "\n". 

The problem is that instead of creating new lines in notepad the result looks like this: 1 2 3 . I have tried using "\n" and "\r" but the result is the same. 

How would I get correct line breaks in notepad
/Hubba


Answer this question

Copy string to clipboard with inserted line breaks

  • Scott Barrett

    System.Environment.NewLine
  • ravinder reddy

    Thanx, it worked fine

    /Hubba

  • rivi

    There might be a serious performance problem here. If you're adding the items of a collection to a string, you'll create a new string object in each iteration:
    // BAD:
    string result = "";
    foreach (int value in someList)
    {
        result += i.ToString() + Environment.NewLine;
    }
    return result;

    // GOOD:
    System.IO.StringWriter result = new System.IO.StringWriter();
    foreach (int value in someList)
    {
        result.WriteLine(i.ToString());
    }
    return result.ToString();

    The StringWriter uses a StringBuilder for performant string concatenation. You could use the StringBuilder directly, but the StringWriter gives you the WriteLine function, so you don't have to care about the newline character.

  • Copy string to clipboard with inserted line breaks