Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion concepts/strings/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"authors": [
"mirkoperillo"
],
"contributors": []
"contributors": [
"fapdash"
]
}
44 changes: 42 additions & 2 deletions concepts/strings/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ 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
String multilineHtml = "<html>\n <body>\n <h1>Hello, World!</h1>\n </body>\n</html>\n";
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something still seems weird about the flow because we revisit line separators later. Perhaps we could add a sentence like Alternatively, we can use String.format or ``System.lineSeparator()` to add system independent line separator (discussed later).

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
String multilineHtml = """
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
""";
// => "<html>\n <body>\n <h1>Hello, World!</h1>\n </body>\n</html>\n"
```

Finally, there are many ways to concatenate a string.
The simplest one is the `+` operator:

Expand All @@ -35,15 +55,35 @@ 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)
```

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]
- 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()