From e713aaef239be6879339ae5716035343ae9a7b68 Mon Sep 17 00:00:00 2001 From: fap <459631+fapdash@users.noreply.github.com> Date: Sun, 27 Apr 2025 17:42:42 +0200 Subject: [PATCH 1/7] 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 --- concepts/strings/.meta/config.json | 4 +++- concepts/strings/about.md | 38 ++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/concepts/strings/.meta/config.json b/concepts/strings/.meta/config.json index f43806375..6b509f9cf 100644 --- a/concepts/strings/.meta/config.json +++ b/concepts/strings/.meta/config.json @@ -3,5 +3,7 @@ "authors": [ "mirkoperillo" ], - "contributors": [] + "contributors": [ + "fapdash" + ] } diff --git a/concepts/strings/about.md b/concepts/strings/about.md index 1dc4e8205..01fb69ca4 100644 --- a/concepts/strings/about.md +++ b/concepts/strings/about.md @@ -22,6 +22,29 @@ String escaped = "c:\\test.txt"; // => c:\test.txt ``` +To put a newline character in a string, use the `\n` escape code (`\r\n` on Windows): + +```java +"\n \n

Hello, World!

\n \n\n" +``` + +For code that should work on different operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string. + +To comfortable work with texts that contain a lot of newlines you can use [Text Blocks](text-blocks). +These multi-line strings are delimited by triple double quote (`"`) characters. + + +```java +String multilineHtml = """ + + +

Hello, World!

+ + +"""; +// => "\n \n

Hello, World!

\n \n\n" +``` + Finally, there are many ways to concatenate a string. The simplest one is the `+` operator: @@ -35,15 +58,26 @@ For any string formatting more complex than simple concatenation, `String.format ```java String name = "Jane"; -String.format("Hello %s!",name); +String.format("Hello %s!", name); // => "Hello Jane!" ``` -Other possibilities are: +The conversion `%n` in a format string inserts a system-dependent line separator. + +```java +String name = "Jane"; +String.format("Hello,%n%s!", name); +// => "Hello,\nJane!" (Linux, macOS) +// => "Hello,\r\nJane!" (Windows) +``` + +Other possibilities to build more complex strings are: - use [`StringBuilder` class][string-builder] - use [`String.concat` method][string-concat] [string-class]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html +[text-blocks]: https://openjdk.org/projects/amber/guides/text-blocks-guide [string-builder]: https://docs.oracle.com/javase/tutorial/java/data/buffers.html [string-concat]: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#concat-java.lang.String- +[system-line-separator]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#lineSeparator() From 3bf34e67f531f279727c482a687e2f5d83a715b3 Mon Sep 17 00:00:00 2001 From: fap <459631+fapdash@users.noreply.github.com> Date: Sun, 27 Apr 2025 17:58:15 +0200 Subject: [PATCH 2/7] Rewording --- concepts/strings/about.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/strings/about.md b/concepts/strings/about.md index 01fb69ca4..c9d86ec02 100644 --- a/concepts/strings/about.md +++ b/concepts/strings/about.md @@ -28,7 +28,7 @@ To put a newline character in a string, use the `\n` escape code (`\r\n` on Wind "\n \n

Hello, World!

\n \n\n" ``` -For code that should work on different operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string. +For code that should work on varying operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string. To comfortable work with texts that contain a lot of newlines you can use [Text Blocks](text-blocks). These multi-line strings are delimited by triple double quote (`"`) characters. From 0edaa9f969b6ac0623b318007863e66bf34badb9 Mon Sep 17 00:00:00 2001 From: fap <459631+fapdash@users.noreply.github.com> Date: Sun, 27 Apr 2025 18:09:51 +0200 Subject: [PATCH 3/7] Fix markdown linting errors --- concepts/strings/about.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/concepts/strings/about.md b/concepts/strings/about.md index c9d86ec02..7d106cba1 100644 --- a/concepts/strings/about.md +++ b/concepts/strings/about.md @@ -30,10 +30,9 @@ To put a newline character in a string, use the `\n` escape code (`\r\n` on Wind For code that should work on varying operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string. -To comfortable work with texts that contain a lot of newlines you can use [Text Blocks](text-blocks). +To comfortable work with texts that contain a lot of newlines you can use [Text Blocks][text-blocks]. These multi-line strings are delimited by triple double quote (`"`) characters. - ```java String multilineHtml = """ From 566f303e2e95354fd0d1c8c9e84a10a0867ecb04 Mon Sep 17 00:00:00 2001 From: fap <459631+fapdash@users.noreply.github.com> Date: Sun, 27 Apr 2025 18:45:24 +0200 Subject: [PATCH 4/7] Clarify when to use system dependent line separators --- concepts/strings/about.md | 1 + 1 file changed, 1 insertion(+) diff --git a/concepts/strings/about.md b/concepts/strings/about.md index 7d106cba1..30f43a83f 100644 --- a/concepts/strings/about.md +++ b/concepts/strings/about.md @@ -29,6 +29,7 @@ To put a newline character in a string, use the `\n` escape code (`\r\n` on Wind ``` For code that should work on varying operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string. +This is important if you're writing to files that will be read on the same system. To comfortable work with texts that contain a lot of newlines you can use [Text Blocks][text-blocks]. These multi-line strings are delimited by triple double quote (`"`) characters. From 01c5a650cfd0f7e92d6189412b83523ffc7d32f7 Mon Sep 17 00:00:00 2001 From: FAP <459631+fapdash@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:35:35 +0100 Subject: [PATCH 5/7] Fix typo in concepts/strings/about.md Co-authored-by: Kah Goh --- concepts/strings/about.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/strings/about.md b/concepts/strings/about.md index 30f43a83f..c8c51d383 100644 --- a/concepts/strings/about.md +++ b/concepts/strings/about.md @@ -31,7 +31,7 @@ To put a newline character in a string, use the `\n` escape code (`\r\n` on Wind For code that should work on varying operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string. This is important if you're writing to files that will be read on the same system. -To comfortable work with texts that contain a lot of newlines you can use [Text Blocks][text-blocks]. +To comfortably work with texts that contain a lot of newlines you can use [Text Blocks][text-blocks]. These multi-line strings are delimited by triple double quote (`"`) characters. ```java From 1ef8e46c7bde5358f1449b026fa16b07d7801e7f Mon Sep 17 00:00:00 2001 From: FAP <459631+fapdash@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:38:37 +0100 Subject: [PATCH 6/7] Update concepts/strings/about.md Co-authored-by: Kah Goh --- concepts/strings/about.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/strings/about.md b/concepts/strings/about.md index c8c51d383..12c5cf257 100644 --- a/concepts/strings/about.md +++ b/concepts/strings/about.md @@ -25,7 +25,7 @@ String escaped = "c:\\test.txt"; To put a newline character in a string, use the `\n` escape code (`\r\n` on Windows): ```java -"\n \n

Hello, World!

\n \n\n" +String multilineHtml = "\n \n

Hello, World!

\n \n\n"; ``` For code that should work on varying operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string. From 7b5c139bc7a4139fb7483fb4c92676bcdc5064d5 Mon Sep 17 00:00:00 2001 From: fap <459631+fapdash@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:53:41 +0100 Subject: [PATCH 7/7] Move System.lineSeperator after string concatination and add example --- concepts/strings/about.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/concepts/strings/about.md b/concepts/strings/about.md index 12c5cf257..faaeda33f 100644 --- a/concepts/strings/about.md +++ b/concepts/strings/about.md @@ -28,9 +28,6 @@ To put a newline character in a string, use the `\n` escape code (`\r\n` on Wind String multilineHtml = "\n \n

Hello, World!

\n \n\n"; ``` -For code that should work on varying operating systems Java offers [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator string. -This is important if you're writing to files that will be read on the same system. - To comfortably work with texts that contain a lot of newlines you can use [Text Blocks][text-blocks]. These multi-line strings are delimited by triple double quote (`"`) characters. @@ -71,6 +68,15 @@ String.format("Hello,%n%s!", name); // => "Hello,\r\nJane!" (Windows) ``` +Alternatively you can call the function [`System.lineSeparator()`][system-line-separator], which returns the system-dependent line separator as a string. + +```java +String name = "Jane"; +"Hello," + System.lineSeparator() + name; +// => "Hello,\nJane!" (Linux, macOS) +// => "Hello,\r\nJane!" (Windows) +``` + Other possibilities to build more complex strings are: - use [`StringBuilder` class][string-builder]