admin管理员组文章数量:1384254
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.
-
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
3 Answers
Reset to default 4The 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
本文标签: angularPrint new line character as string in javascriptStack Overflow
版权声明:本文标题:angular - Print new line character as string in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744443771a2606562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论