Skip to content

Commit 16b9df0

Browse files
committed
strings - Expand concept by newline and text blocks
- Add multi-line strings / text blocks (Java 15) - Add explanation of newlines / end of line chars, so students can understand the motivation behind text blocks
1 parent df966c3 commit 16b9df0

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

concepts/strings/.meta/config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
"authors": [
44
"mirkoperillo"
55
],
6-
"contributors": []
6+
"contributors": [
7+
"fapdash"
8+
]
79
}

concepts/strings/about.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@ String escaped = "c:\\test.txt";
2222
// => c:\test.txt
2323
```
2424

25+
To put a newline character in a string, use the `\n` escape code (`\r\n` on Windows):
26+
27+
```java
28+
"<html>\n <body>\n <h1>Hello, World!</h1>\n </body>\n</html>\n"
29+
```
30+
31+
For code that should work on different operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string.
32+
33+
To comfortable work with texts that contain a lot of newlines you can use [Text Blocks](text-blocks).
34+
These multi-line strings are delimited by triple double quote (`"`) characters.
35+
36+
37+
```java
38+
String multilineHtml = """
39+
<html>
40+
<body>
41+
<h1>Hello, World!</h1>
42+
</body>
43+
</html>
44+
""";
45+
// => "<html>\n <body>\n <h1>Hello, World!</h1>\n </body>\n</html>\n"
46+
```
47+
2548
Finally, there are many ways to concatenate a string.
2649
The simplest one is the `+` operator:
2750

@@ -35,15 +58,26 @@ For any string formatting more complex than simple concatenation, `String.format
3558

3659
```java
3760
String name = "Jane";
38-
String.format("Hello %s!",name);
61+
String.format("Hello %s!", name);
3962
// => "Hello Jane!"
4063
```
4164

42-
Other possibilities are:
65+
The conversion `%n` in a format string inserts a system-dependent line separator.
66+
67+
```java
68+
String name = "Jane";
69+
String.format("Hello,%n%s!", name);
70+
// => "Hello,\nJane!" (Linux, macOS)
71+
// => "Hello,\r\nJane!" (Windows)
72+
```
73+
74+
Other possibilities to build more complex strings are:
4375

4476
- use [`StringBuilder` class][string-builder]
4577
- use [`String.concat` method][string-concat]
4678

4779
[string-class]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html
80+
[text-blocks]: https://openjdk.org/projects/amber/guides/text-blocks-guide
4881
[string-builder]: https://docs.oracle.com/javase/tutorial/java/data/buffers.html
4982
[string-concat]: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#concat-java.lang.String-
83+
[system-line-separator]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#lineSeparator()

0 commit comments

Comments
 (0)