-
-
Notifications
You must be signed in to change notification settings - Fork 710
strings - Expand concept by newline and text blocks #2941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,7 @@ | |
"authors": [ | ||
"mirkoperillo" | ||
], | ||
"contributors": [] | ||
"contributors": [ | ||
"fapdash" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
"<html>\n <body>\n <h1>Hello, World!</h1>\n </body>\n</html>\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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't using the |
||||||
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]. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the result string line looks a little strange with it all being on one line with the
Suggested change
|
||||||
``` | ||||||
|
||||||
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought to assign to variable to keep consistent with the other examples.