4

I need to insert a Carriage Return in a String. As far as I know the \r should do it but here is the problem:

I wrote this in the browser console: 1\r2 then I get: 12 in return. Now I copy/paste it on Notepad++ https://i.sstatic.net/P3AQO.jpg

There is a CR and a LF there. How I can add just a CR?

Note that you can replace the LF(\n) in notepad, save the file and the LFs are gone.

5
  • Why do you want this? Commented Oct 10, 2017 at 18:34
  • Probably Notepad++ added an LF by itself. Commented Oct 10, 2017 at 18:34
  • i make a CSV file and send it via Email, the line delimiters must be <CR> only. Commented Oct 10, 2017 at 18:44
  • Do you know you can change the line delimiter to CR only in notepad++? Menu Edit - EOL Conversion - Old Mac Format. Commented Oct 10, 2017 at 18:51
  • Yes, but that does not modify the file, just the way that you look at it in notepad++ Commented Oct 10, 2017 at 19:11

2 Answers 2

5

In ES6 with string templates you just introduce the carriage return as written text. Also you can use \n to mark carriage return in quoted strings.

const str = `l
e`;

console.log(str);

const str2 = 'l\ne';

console.log(str2);

Sign up to request clarification or add additional context in comments.

Comments

0

Usually, carriage returns often go like this...

\n

and you can use more than one to go down multiple lines.

It's the equivalent of pressing the enter/return key after you type one line and you want to move to the next one.

For example,

var astring = "This is a \n test";

print(astring);

You would get:

This is a
test

Hope it answers your question,

Alexander B.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.