Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a String with carriage returns?

Tags:

For a JUnit test I need a String which consists of multiple lines. But all I get is a single lined String. I tried the following:

    String str = ";;;;;;\n" +                  "Name, number, address;;;;;;\n" +                   "01.01.12-16.02.12;;;;;;\n" +                   ";;;;;;\n" +                   ";;;;;;"; 

I also tried \n\r instead of \n. System.getProperty("line.separator") doesn't work too. it produces a \n in String and no carriage return. So how can I solve that?

like image 366
Bevor Avatar asked Apr 03 '12 08:04

Bevor


People also ask

What is the character code for carriage return?

CR (character : \r, Unicode : U+000D, ASCII : 13, hex : 0x0d) : This is simply the 'r' character. This character is commonly known as 'Carriage Return'. As matter of fact, \r has also has a different meaning.

How do you split a string with a new line?

Split String by Newline in Java 8. Java 8 provides an “\R” pattern that matches any Unicode line-break sequence and covers all the newline characters for different operating systems. Therefore, we can use the “\R” pattern instead of “\\r?\\ n|\\r” in Java 8 or higher.

What does \r do in Java?

'\r' is the representation of the special character CR (carriage return), it moves the cursor to the beginning of the line. '\n'(line feed) moves the cursor to the next line . On windows both are combined as \r\n to indicate an end of line (ie, move the cursor to the beginning of the next line).

How do I add a line break to a string 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.


2 Answers

It depends on what you mean by "multiple lines". Different operating systems use different line separators.

In Java, \r is always carriage return, and \n is line feed. On Unix, just \n is enough for a newline, whereas many programs on Windows require \r\n. You can get at the platform default newline use System.getProperty("line.separator") or use String.format("%n") as mentioned in other answers.

But really, you need to know whether you're trying to produce OS-specific newlines - for example, if this is text which is going to be transmitted as part of a specific protocol, then you should see what that protocol deems to be a newline. For example, RFC 2822 defines a line separator of \r\n and this should be used even if you're running on Unix. So it's all about context.

like image 149
Jon Skeet Avatar answered Oct 27 '22 15:10

Jon Skeet


The fastest way I know to generate a new-line character in Java is: String.format("%n")

Of course you can put whatever you want around the %n like:

String.format("line1%nline2")

Or even if you have a lot of lines:

String.format("%s%n%s%n%s%n%s", "line1", "line2", "line3", "line4")

like image 25
Jesus is Lord Avatar answered Oct 27 '22 15:10

Jesus is Lord