Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace \n with <br />

I'm parsing text from file with Python. I have to replace all newlines (\n) with
cause this text will build html-content. For example, here is some line from file:

'title\n' 

Now I do:

thatLine.replace('\n', '<br />') print thatLine 

And I still see the text with newline after it.

like image 972
Max Frai Avatar asked Dec 06 '10 17:12

Max Frai


People also ask

How do I replace all line breaks in a string with br /> elements?

The RegEx is used with the replace() method to replace all the line breaks in string with <br>. The pattern /(\r\n|\r|\n)/ checks for line breaks. The pattern /g checks across all the string occurrences.

What is br/> in Java?

Replace new line (\n) with HTML br tag in string using Java uses the \n character, also known as Line Feed (LF) character, to move the cursor to the next line. Windows uses \r\n characters to specify the start of the line, sometimes also called Carriage Return and Line Feed (CRLF).


1 Answers

thatLine = thatLine.replace('\n', '<br />')

str.replace() returns a copy of the string, it doesn't modify the string you pass in.

like image 164
Falmarri Avatar answered Sep 21 '22 22:09

Falmarri