Rectangle 27 0

Java multiline string?


Note
Rectangle 27 0

Java multiline string?


Stephen Colebourne has created a proposal for adding multi-line strings in Java 7.

any change in 2012?

Note
Rectangle 27 0

Java multiline string?


See Java Stringfier. Turns your text into a StringBuilder java block escaping if needed.

it's not preserving newlines...

Note
Rectangle 27 0

Java multiline string?


String s2 = foobar.SWrap.bar ();
package foobar
object SWrap {
  def bar = """John said: "This is
  a test
  a bloody test,
  my dear." and closed the door.""" 
}
sed '1s/^/String s = \"/;2,$s/^/\t+ "/;2,$s/$/"/' file > file.java

(note the quotes inside the string) and from java:

Another approach, if you often handle long text, which should be placed in your sourcecode, might be a script, which takes the text from an external file, and wrappes it as a multiline-java-String like this:

You may use scala-code, which is compatible to java, and allows multiline-Strings enclosed with """:

so that you may cut-and-paste it easily into your source.

Note
Rectangle 27 0

Java multiline string?


String str = "paste your text here";

In Eclipse if you turn on the option "Escape text when pasting into a string literal" (in Preferences > Java > Editor > Typing) and paste a multi-lined string whithin quotes, it will automatically add " and \n" + for all your lines.

Note
Rectangle 27 0

Java multiline string?


String s = "It was the best of times, it was the worst of times,\n"
         + "it was the age of wisdom, it was the age of foolishness,\n"
         + "it was the epoch of belief, it was the epoch of incredulity,\n"
         + "it was the season of Light, it was the season of Darkness,\n"
         + "it was the spring of hope, it was the winter of despair,\n"
         + "we had everything before us, we had nothing before us";
String s = String.format("%s\n%s\n%s\n%s\n%s\n%s"
         , "It was the best of times, it was the worst of times,"
         , "it was the age of wisdom, it was the age of foolishness,"
         , "it was the epoch of belief, it was the epoch of incredulity,"
         , "it was the season of Light, it was the season of Darkness,"
         , "it was the spring of hope, it was the winter of despair,"
         , "we had everything before us, we had nothing before us"
);
String s = String.join("\n"
         , "It was the best of times, it was the worst of times,"
         , "it was the age of wisdom, it was the age of foolishness,"
         , "it was the epoch of belief, it was the epoch of incredulity,"
         , "it was the season of Light, it was the season of Darkness,"
         , "it was the spring of hope, it was the winter of despair,"
         , "we had everything before us, we had nothing before us"
);
String s = new StringBuilder()
           .append("It was the best of times, it was the worst of times,\n")
           .append("it was the age of wisdom, it was the age of foolishness,\n")
           .append("it was the epoch of belief, it was the epoch of incredulity,\n")
           .append("it was the season of Light, it was the season of Darkness,\n")
           .append("it was the spring of hope, it was the winter of despair,\n")
           .append("we had everything before us, we had nothing before us")
           .toString();
String.format()
String.join()
StringBuilder

@BlessedGeek: the question at hand was about what options were available in the Java language. It didn't mention anything about the type of data going into the string. If there is a better solution then you can post it as an answer. It sounds like Josh Curren's solution would be better for your situation. If you are just upset that the language doesn't support multiline literals then this is the wrong place to complain about it.

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.

Furthermore, the first version will be automatically concatenated by the compiler, since all the strings are known at compile time. Even if the strings are not known at compile time, it's no slower than StringBuilder or String.format(). The only reason to avoid concatenation with +'s is if you're doing it in a loop.

If you want the newline for your particular system, you either need to use System.getProperty("line.separator"), or you can use %n in String.format.

It sounds like you want to do a multiline literal, which does not exist in Java.

String.format is not efficient compared to other two examples

The problem with the String.format version is that you have to keep the format in sync with the number of lines.

This answer is a very inappropriate solution to the question at hand. We have 2000 line SAS macros or bunches of 200 line SQL queries which we wish to copy and paste. To suggest that we use +"" concat to transform those multiline text into StringBuffer appends is ridiculous.

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.

Note
Rectangle 27 0

Java multiline string?


String s = "It was the best of times, it was the worst of times,\n"
         + "it was the age of wisdom, it was the age of foolishness,\n"
         + "it was the epoch of belief, it was the epoch of incredulity,\n"
         + "it was the season of Light, it was the season of Darkness,\n"
         + "it was the spring of hope, it was the winter of despair,\n"
         + "we had everything before us, we had nothing before us";
String s = String.format("%s\n%s\n%s\n%s\n%s\n%s"
         , "It was the best of times, it was the worst of times,"
         , "it was the age of wisdom, it was the age of foolishness,"
         , "it was the epoch of belief, it was the epoch of incredulity,"
         , "it was the season of Light, it was the season of Darkness,"
         , "it was the spring of hope, it was the winter of despair,"
         , "we had everything before us, we had nothing before us"
);
String s = String.join("\n"
         , "It was the best of times, it was the worst of times,"
         , "it was the age of wisdom, it was the age of foolishness,"
         , "it was the epoch of belief, it was the epoch of incredulity,"
         , "it was the season of Light, it was the season of Darkness,"
         , "it was the spring of hope, it was the winter of despair,"
         , "we had everything before us, we had nothing before us"
);
String s = new StringBuilder()
           .append("It was the best of times, it was the worst of times,\n")
           .append("it was the age of wisdom, it was the age of foolishness,\n")
           .append("it was the epoch of belief, it was the epoch of incredulity,\n")
           .append("it was the season of Light, it was the season of Darkness,\n")
           .append("it was the spring of hope, it was the winter of despair,\n")
           .append("we had everything before us, we had nothing before us")
           .toString();
String.format()
String.join()
StringBuilder

@BlessedGeek: the question at hand was about what options were available in the Java language. It didn't mention anything about the type of data going into the string. If there is a better solution then you can post it as an answer. It sounds like Josh Curren's solution would be better for your situation. If you are just upset that the language doesn't support multiline literals then this is the wrong place to complain about it.

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.

Furthermore, the first version will be automatically concatenated by the compiler, since all the strings are known at compile time. Even if the strings are not known at compile time, it's no slower than StringBuilder or String.format(). The only reason to avoid concatenation with +'s is if you're doing it in a loop.

If you want the newline for your particular system, you either need to use System.getProperty("line.separator"), or you can use %n in String.format.

It sounds like you want to do a multiline literal, which does not exist in Java.

String.format is not efficient compared to other two examples

The problem with the String.format version is that you have to keep the format in sync with the number of lines.

This answer is a very inappropriate solution to the question at hand. We have 2000 line SAS macros or bunches of 200 line SQL queries which we wish to copy and paste. To suggest that we use +"" concat to transform those multiline text into StringBuffer appends is ridiculous.

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.

Note
Rectangle 27 0

Java multiline string?


String str = "paste your text here";

Do you generally leave in the \rs that Eclipse puts in on Windows?

In Eclipse if you turn on the option "Escape text when pasting into a string literal" (in Preferences > Java > Editor > Typing) and paste a multi-lined string whithin quotes, it will automatically add " and \n" + for all your lines.

intelij also does this by default when you paste into ""s

Note
Rectangle 27 0

Java multiline string?


public final class MultilineStringUsage {

  /**
  <html>
    <head/>
    <body>
      <p>
        Hello<br/>
        Multiline<br/>
        World<br/>
      </p>
    </body>
  </html>
  */
  @Multiline
  private static String html;

  public static void main(final String[] args) {
    System.out.println(html);
  }
}

And you probably have to configure Eclipse to not reformat automatically your javadoc comments.

Does that require the class using the multiline string to be final? Also, is any setup required when developing and executing code from Eclipse? The reference URL mentions setup requirements for Maven for annotation processing. I can't figure out what might be needed, if any in Eclipse.

Edit: The above URL seems to be broken. A project inspired from that work is hosted on GitHub:

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.

The annotation is livable - but it seems there were also a hard dependency on maven? That part takes away much of the value of heredoc's which are to simplify the management of small pieces of text.

The drawback is that you have to activate the corresponding (provided) annotation processor.

This annotation-processor is not supported anymore by Eclipse Neon

This is an old thread, but a new quite elegant solution (with only one drawback) is to use a custom annotation.

You can do this entirely in eclipse. The link that @SRG posted above points you to this link. If you are using eclipse, then a minute of setup and it is working.

Note
Rectangle 27 0

Java multiline string?


public final class MultilineStringUsage {

  /**
  <html>
    <head/>
    <body>
      <p>
        Hello<br/>
        Multiline<br/>
        World<br/>
      </p>
    </body>
  </html>
  */
  @Multiline
  private static String html;

  public static void main(final String[] args) {
    System.out.println(html);
  }
}

And you probably have to configure Eclipse to not reformat automatically your javadoc comments.

Does that require the class using the multiline string to be final? Also, is any setup required when developing and executing code from Eclipse? The reference URL mentions setup requirements for Maven for annotation processing. I can't figure out what might be needed, if any in Eclipse.

Edit: The above URL seems to be broken. A project inspired from that work is hosted on GitHub:

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.

The annotation is livable - but it seems there were also a hard dependency on maven? That part takes away much of the value of heredoc's which are to simplify the management of small pieces of text.

The drawback is that you have to activate the corresponding (provided) annotation processor.

This annotation-processor is not supported anymore by Eclipse Neon

This is an old thread, but a new quite elegant solution (with only one drawback) is to use a custom annotation.

You can do this entirely in eclipse. The link that @SRG posted above points you to this link. If you are using eclipse, then a minute of setup and it is working.

Note