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

Copy string to clipboard with inserted line breaks
Scott Barrett
ravinder reddy
/Hubba
rivi
// 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.