I want to put line breaks on a string on angular.ts side but '\n' is not working.
var msg = "hello friend!" + '\n'
msg+= "You fine there"
console.log(msg);
What must i do to achieve the string line break?
I want to put line breaks on a string on angular.ts side but '\n' is not working.
var msg = "hello friend!" + '\n'
msg+= "You fine there"
console.log(msg);
What must i do to achieve the string line break?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question.
Sam HobbsPosted May 16, 2024, 5:47 PM
For Unix and Linux
\n(line feed) is a line break. In Apple operating systems\r(carriage return) is a line break. In Windows, text in a HTML element and for email\r\nis a line break. The reason a combination of line feed and carriage return is used is that for printers a new line requires that combination. For HTML however we need a
as others have said unless it is text in a text element. If
does not work then try\r\n.Beginner C and C++ programmers might think that
\nis a line break but the C and C++ compilers convert\nto\r\nfor Windows.Jaimin ShethiyaPosted May 16, 2024, 2:00 AM
Hello Austin,
Have you checked my solution, if not then just check it, it works as your expectation.
Thanks
Jaimin ShethiyaPosted May 15, 2024, 7:09 AM
Hello Austin,
Try the below steps.
In typescript side,
In html side,
Thanks
Jignesh KumarPosted May 15, 2024, 5:15 AM
Hello,
Please check online typescript editor,
Visit : https://stackblitz.com/edit/typescript-playground?file=index.ts
Remove all content and add your typescript code
Check it is working as expected.
Naimish MakwanaPosted May 15, 2024, 3:47 AM
In JavaScript and TypeScript, the newline character
\ndoes indeed create a new line. However, if you’re trying to view the output in an HTML context (like in the browser console or on a webpage), HTML treats whitespace such as\nas a single space.To see the line breaks in an HTML context, you can replace
\nwith
, which is the HTML tag for a line break. But remember, this will only work if your string is being outputted as HTML.Here’s how you can do it:
If you want to see the line breaks in the console, you should be able to see them with
\n. If you’re not seeing them, it might be an issue with how your console is displaying the output. Try viewing the output in a different console or log viewer to see if that makes a difference.Remember, the above solution is for Angular TypeScript files. If you’re working with Angular templates (HTML files), you might need a different approach.
Thanks
Tahir AnsariPosted May 15, 2024, 12:50 AM
Output
Austin MutsPosted May 14, 2024, 4:29 PM
we do not want to display this on html side. The string is displayed on angular.ts side . So that answer is not correct.