Here in this article, I will show you 4 different ways to use multiline strings in Java. This can be done in the following ways:
- Text Block
- Concatenation Operator
- StringBuilder Class
- Stream API and Collectors.joining()
Let’s discuss them one by one in detail with example code.
Method 1: Text Block
It is a new feature added in Java 15 version. In this example, we create a text block using triple quotes (“””). Text block is the best way to use multiline strings as the text indentation and formatting are preserved.
public class MultiLineString {
public static void main(String[] args) {
String multiLineStr = """
This is an example of multiline string.
Here text formatting is preserved.
Text block can be used for multiline string.
""";
System.out.println(multiLineStr);
}
}
Output:
This is an example of multiline string.
Here text formatting is preserved.
Text block can be used for multiline string.
Method 2: Concatenation Operator
The concatenation operator (+) is used to combine various string literals. ‘\n’ escape character is used to mark the end of each line.
public class MultiLineString {
public static void main(String[] args) {
String multiLineStr = "This is an example of multiline string.\n" +
"This is an example of multiline string.\n" +
"+ operator can be used for multiline string.";
System.out.println(multiLineStr);
}
}
Output:
This is an example of multiline string.
This is an example of multiline string.
+ operator can be used for multiline string.
Method 3: StringBuilder Class
In this example, we use append() method of the StringBuilder class to append various lines of text. After that toString() method is used on the object to convert it to a multiline string.
public class MultiLineString {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("This is an example of multiline string.\n");
sb.append("This is an example of multiline string.\n");
sb.append("StringBuilder can be used for multiline string.\n");
String multiLineStr = sb.toString();
System.out.println(multiLineStr);
}
}
Output:
This is an example of multiline string.
This is an example of multiline string.
StringBuilder can be used for multiline string.
Method 4: Stream API and Collectors.joining()
Here we defined a list of strings and used Stream API and Collectors.joining() method to join all the strings. ‘\n’ is used as a delimiter argument to indicate the end of each line.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MultiLineString {
public static void main(String[] args) {
List<String> lines = Arrays.asList("This is an example of multiline string.",
"This is an example of multiline string.",
"Stream API can be used for multiline string.");
String multiLineStr = lines.stream().collect(Collectors.joining("\n"));
System.out.println(multiLineStr);
}
}
Output:
This is an example of multiline string.
This is an example of multiline string.
Stream API can be used for multiline string.
I hope you got an idea about using the multiline strings in java. You can ask your queries in the comment section below.