Skip to content

Commit 7b5c139

Browse files
committed
Move System.lineSeperator after string concatination and add example
1 parent 1ef8e46 commit 7b5c139

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

concepts/strings/about.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ To put a newline character in a string, use the `\n` escape code (`\r\n` on Wind
2828
String multilineHtml = "<html>\n <body>\n <h1>Hello, World!</h1>\n </body>\n</html>\n";
2929
```
3030

31-
For code that should work on varying operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string.
32-
This is important if you're writing to files that will be read on the same system.
33-
3431
To comfortably work with texts that contain a lot of newlines you can use [Text Blocks][text-blocks].
3532
These multi-line strings are delimited by triple double quote (`"`) characters.
3633

@@ -71,6 +68,15 @@ String.format("Hello,%n%s!", name);
7168
// => "Hello,\r\nJane!" (Windows)
7269
```
7370

71+
Alternatively you can call the function [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator as a string.
72+
73+
```java
74+
String name = "Jane";
75+
"Hello," + System.lineSeparator() + name;
76+
// => "Hello,\nJane!" (Linux, macOS)
77+
// => "Hello,\r\nJane!" (Windows)
78+
```
79+
7480
Other possibilities to build more complex strings are:
7581

7682
- use [`StringBuilder` class][string-builder]

0 commit comments

Comments
 (0)