Create multiline strings in Java
This post will discuss how to create multiline strings in Java.
1. Using String concat (+)
A simple solution to concat strings that span multiple lines is using the String concatenation operator (+). This approach can be used if string concatenation does not take place within a loop, since the compiler optimizes the string concatenation using the + operator.
This is demonstrated below. Note that each line is terminated by \n. If you need the system-dependent line separator string, use the System.lineSeparator() method.
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { String s = "Line 1\n" + "Line 2\n" + "Line 3"; System.out.println(s); } } |
Output:
Line 1
Line 2
Line 3
If you’re using IntelliJ IDEA, you don’t have to manually write the complete string. IntelliJ IDEA automatically adds "..\n" + for all lines when you paste the multiline string within "". In Eclipse, this option is not enabled by default. To enable it, just select “Escape text when pasting into a string literal” in Window -> preferences -> java -> Editor -> Typing.
2. Using StringBuilder
You should consider using the StringBuilder over the String concat operator (+) if the multiline string is constructed within a loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { String[] lines = { "Line 1", "Line 2", "Line 3" }; StringBuilder sb = new StringBuilder(); for (String line: lines) { sb.append(line).append(System.lineSeparator()); } System.out.println(sb.toString()); } } |
Output:
Line 1
Line 2
Line 3
3. Using String.format() method
If you have less number of lines in the multiline string, consider using the String.format() method that returns a formatted string according to the specified format arguments. For instance,
|
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { String s = String.format("%s\n%s\n%s", "Line 1", "Line 2", "Line 3"); System.out.println(s); } } |
Output:
Line 1
Line 2
Line 3
4. Using String.join() method
Starting with Java 8, you can use the String.join() method that returns a new String composed of specified strings joined together with a copy of the specified delimiter. For example,
|
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { String s = String.join(System.lineSeparator(), "Line 1", "Line 2", "Line 3"); System.out.println(s); } } |
Output:
Line 1
Line 2
Line 3
5. Java 13 – Using Text Blocks
There is no direct option to create a multiline string literal in Java 12 or less. Java 13, however, allows you to define multiline string literals using text blocks with ease. Note that text blocks are a preview feature and are disabled by default. You can use --enable-preview to enable text blocks.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { String s = """ Line 1 Line 2 Line 3 """; System.out.println(s); } } |
Output:
Line 1
Line 2
Line 3
That’s all about creating multiline strings in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)