How to create a multiline string in Java
In this tutorial, we are going to learn about how to create a multiline string in Java.
Creating a multiline string
To create a multiline string in Java, we can use the built-in join()
method by passing /n
as a first argument to it.
Here is an example:
public class Main
{
public static void main(String[] args) {
String s = String.join("\n",
"This is the way we can",
"create a multiline string",
"in java"
);
System.out.println(s);
}
}
Output:
This is the way we can
create a multiline string
in java