Java
Multi-line String in Java
In Java, the string is basically an object that represents a sequence of char values. An array of characters works the same as a Java string. For example:
char[] ch={'j','a','v','a','t','p','o','i','n','t'};
String s=new String(ch);
is the same as:
String s="javatpoint";
In this blog, we'll learn how to declare multiline strings in Java. Now that Java 15 is out, we can use the new native feature called Text Blocks. We'll also review other methods if we can't use this feature.
Text Blocks:
We can use Text Blocks by declaring the string with """ (three double quote marks):
public String textBlocks() {
return """
Get busy living
or
get busy dying.
--Stephen King""";
}
It is by far the most convenient way to declare a multiline string. Indeed, we don't have to deal with line separators or indentation spaces. This feature is available in Java 15, but also Java 13 and 14 if we enable the preview feature.
In the following sections, we'll review other methods that are suitable if we use a previous version of Java or if Text Blocks aren't applicable.
Getting the Line Separator:
Each operating system can have its own way of defining and recognizing new lines. In Java, it's very easy to get the operating system line separator:
String newLine = System.getProperty("line.separator");
We're going to use this newLine in the following sections to create multiline strings.
String Concatenation:
String concatenation is an easy native method that can be used to create multiline strings:
public String stringConcatenation() {
return "Get busy living"
.concat(newLine)
.concat("or")
.concat(newLine)
.concat("get busy dying.")
.concat(newLine)
.concat("--Stephen King");
}
Using the + operator is another way to achieve the same thing. Java compilers translate concat() and the + operator in the same way:
public String stringConcatenation() {
return "Get busy living"
+ newLine
+ "or"
+ newLine
+ "get busy dying."
+ newLine
+ "--Stephen King";
}
String Join:
Java 8 introduced String#join, which takes a delimiter along with some strings as arguments. It returns a final string having all input strings joined together with the delimiter:
public String stringJoin() {
return String.join(newLine,
"Get busy living",
"or",
"get busy dying.",
"--Stephen King");
}
String Builder:
StringBuilder is a helper class to build Strings. StringBuilder was introduced in Java 1.5 as a replacement for StringBuffer. It's a good choice for building huge strings in a loop:
public String stringBuilder() {
return new StringBuilder()
.append("Get busy living")
.append(newLine)
.append("or")
.append(newLine)
.append("get busy dying.")
.append(newLine)
.append("--Stephen King")
.toString();
}
String Writer:
StringWriter is another method that we can utilize to create a multiline string. We don't need newLine here because we use PrintWriter. The println function automatically adds new lines:
public String stringWriter() {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
printWriter.println("Get busy living");
printWriter.println("or");
printWriter.println("get busy dying.");
printWriter.println("--Stephen King");
return stringWriter.toString();
}
Conclusion
In this blog post, we learned several methods to build multiline strings in Java. The good news is that Java 15 has native support for multiline strings via Text Blocks. All the other methods reviewed can be used in Java 15 or any previous version.
We, at Oodles, provide custom enterprise web development services to help cross-industry businesses get by their routine operational complexities. Contact us at [email protected] to learn more about our enterprise web solutions.