Coming from Perl, I sure am missing the "here-document" means of creating a multi-line string in source code:
$string = <<"EOF" # create a three-line string
text
text
text
EOF
In Java, I have to have cumbersome quotes and plus signs on every line as I concatenate my multiline string from scratch.
What are some better alternatives? Define my string in a properties file?
Edit: Two answers say StringBuilder.append() is preferable to the plus notation. Could anyone elaborate as to why they think so? It doesn't look more preferable to me at all. I'm looking for a way around the fact that multiline strings are not a first-class language construct, which means I definitely don't want to replace a first-class language construct (string concatenation with plus) with method calls.
Edit: To clarify my question further, I'm not concerned about performance at all. I'm concerned about maintainability and design issues.
This is an old thread, but a new quite elegant solution (with only 4 little drawbacks) is to use a custom annotation.
Check : http://www.adrianwalker.org/2011/12/java-multiline-string.html
A project inspired from that work is hosted on GitHub:
https://github.com/benelog/multiline
Example of Java code:
The drawbacks are
<%= variable %>
) :-)And you probably have to configure Eclipse/Intellij-Idea to not reformat automatically your Javadoc comments.
One may find this weird (Javadoc comments are not designed to embed anything other than comments), but as this lack of multiline string in Java is really annoying in the end, I find this to be the least worst solution.
In the IntelliJ IDE you just need to type:
Then position your cursor inside the quotation marks and paste your string. The IDE will expand it into multiple concatenated lines.
Use
Properties.loadFromXML(InputStream)
. There's no need for external libs.Better than a messy code (since maintainability and design are your concern), it is preferable not to use long strings.
Start by reading xml properties:
then you can use your multiline string in a more maintainable way...
MultiLine.xml` gets located in the same folder YourClass:
PS.: You can use
<![CDATA["
..."]]>
for xml-like string.String.join
Java 8 added a new static method to
java.lang.String
which offers a slightly better alternative:String.join( CharSequence delimiter , CharSequence... elements )
Using it:
It sounds like you want to do a multiline literal, which does not exist in Java.
Your best alternative is going to be strings that are just
+
'd together. Some other options people have mentioned (StringBuilder, String.format, String.join) would only be preferable if you started with an array of strings.Consider this:
Versus
StringBuilder
:Versus
String.format()
:Versus Java8
String.join()
:If you want the newline for your particular system, you either need to use
System.getProperty("line.separator")
, or you can use%n
inString.format
.Another option is to put the resource in a text file, and just read the contents of that file. This would be preferable for very large strings to avoid unnecessarily bloating your class files.
JEP 326: Raw String Literals will implement multi-line Strings, so you will be able to write something like:
It is planned as a feature preview in JDK 12.