16

I have a script that copies table cells from the browser into the user's clipboard. I loop through each cell and when a new line is needed I use

text += "\n";

If I paste the text into excel, it formats correctly and fills in the proper rows, however if I paste into notepad, it shows a symbol instead of creating a new line:

123□456□789

instead of:

123

456

789

Is there something else I can use that notepad will recognize as a line break?

betafish
  • 342
  • 1
  • 4
  • 10

3 Answers3

40

that's because you need a carriage return and line feed.

text += "\r\n";

The non programming way
open up in WordPad
save
open in notepad

Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
  • 1
    To clarify, "\n" is acceptable on *nix systems but Windows insists on both "\r\n". That's why you'll see some examples on the web that just use a line feed. – Al. Aug 14 '09 at 15:30
  • yes, you are 100% correct in your assumption. Windows handles the whole CRLF thing differently, and the format of the files will be different depending on which system they were created on. It's a hassle, I know. – Robert Greiner Aug 14 '09 at 16:02
  • 2
    Oddly this isn't working for me. Had just '\n' then added '\r\n' and still no dice in Notepad. However when I paste into Notepad++ and turn on show-all-characters I can see each (CR)(LF)! Very strange indeed. – MetaGuru Aug 12 '15 at 18:53
  • Ah ha! My problem was caused by jQuery.val() on a textarea which apparently strips out the line breaks. May it be known! – MetaGuru Aug 12 '15 at 20:03
5

You can use the following:

text += "\r\n";
Matt
  • 74,352
  • 26
  • 153
  • 180
searlea
  • 8,173
  • 4
  • 34
  • 37
0

In my case, \r\n was not working with double qoutes " but instead single qoute ' did the job like

 text += '\r\n';
Airy
  • 5,484
  • 7
  • 53
  • 78