Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a platform-dependent new line character?

How do I get a platform-dependent newline in Java? I can’t use "\n" everywhere.

like image 513
Spoike Avatar asked Oct 16 '08 09:10

Spoike


People also ask

How do you make a new line character?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do you add a new line character in Python?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.

How do you add a new line in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

What is the difference between \n and \r in Java?

\n is a line feed (LF) character, character code 10. \r is a carriage return (CR) character, character code 13. What they do differs from system to system. On Windows, for instance, lines in text files are terminated using CR followed immediately by LF (e.g., CRLF).


2 Answers

Java 7 now has a System.lineSeparator() method.

like image 124
StriplingWarrior Avatar answered Sep 28 '22 01:09

StriplingWarrior


You can use

System.getProperty("line.separator"); 

to get the line separator

like image 28
abahgat Avatar answered Sep 28 '22 01:09

abahgat