最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - Print new line character as string in javascript - Stack Overflow

programmeradmin4浏览0评论

I'm working on a project which deals with strings a lot. I need to show what kind of delimiter is used in strings. I get that value from an API call. How do I print the delimiter to the console?

delimiter = '\n';

I want to print this on screen.

console.log(delimiter);
// prints "
          "

I need it to print \n instead of literally printing newline.

I'm working on a project which deals with strings a lot. I need to show what kind of delimiter is used in strings. I get that value from an API call. How do I print the delimiter to the console?

delimiter = '\n';

I want to print this on screen.

console.log(delimiter);
// prints "
          "

I need it to print \n instead of literally printing newline.

Share Improve this question asked Oct 8, 2021 at 10:41 Prithvi RajPrithvi Raj 2,0011 gold badge15 silver badges36 bronze badges 2
  • 1 delimiter = '\\n';, perhaps? – esqew Commented Oct 8, 2021 at 10:47
  • It is set from API call,Which I have no access to. – Prithvi Raj Commented Oct 8, 2021 at 10:57
Add a ment  | 

3 Answers 3

Reset to default 4

The character \n is an escape sequence used for new lines.

That is why you need to use replace on the string if you want to see it in the console. Like so:

console.log(delimiter.replace('\n', '\\n'));

The extra backslash escapes the \n so it is not longer treated as a newline.

More about escape sequences can be found here:

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#escape_sequences

To display a (multi)line with already wrapped text, use this

发布评论

评论列表(0)

  1. 暂无评论