Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 13 Triple-quote Text Block *WITHOUT* newlines

The Java 13 multi line text block facility with """ delimiters is becoming well known.

However I have a recurring need where I need entire paragraphs without the embedded newlines.

In other words, the following code snippet:

String paragraph =
    """
    aaaa bbbb cccc
    dddd eeee ffff
    gggg hhhh iiii
    """;
System.out.println(paragraph);

produces the following, as you'd expect:

aaaa bbbb cccc
dddd eeee ffff
gggg hhhh iiii

...which is usually tremendously useful. However in my case, for particularly large paragraphs I need it to produce this:

aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii

(....and deal with text flow later.)

Is there a way to establish a "no-newline" parameter for the triple-quote feature?

like image 516
alife Avatar asked Dec 12 '19 17:12

alife


People also ask

How do you use triple quotes in Java?

The opening triple quotes must be followed by a new line. var expr = """ This is a 1-line "string" with "quotes" and no leading spaces in it! """; The position of the closing triple quotes matters.

How do you block text in Java?

Text blocks start with a “”” (three double-quote marks) followed by optional whitespaces and a newline. The most simple example looks like this: String example = """ Example text"""; Note that the result type of a text block is still a String.

How do you add multiple lines to a string in Java?

If you want your string to span multiple lines, you have to concatenate multiple strings: String myString = "This is my string" + " which I want to be " + "on multiple lines."; It gets worse though. If you want your string to actually contain new lines, you need to insert \n after each line.

Which of these is a function of Java text block?

A text block's principalis munus is to provide clarity by way of minimizing the Java syntax required to render a string that spans multiple lines. In earlier releases of the JDK, embedding multi-line code snippets required a tangled mess of explicit line terminators, string concatenations, and delimiters.


3 Answers

The designers of this feature realized this requirement as well (see 'New escape sequences' in JEP368). So, with the latest early access build for JDK 14 you can use a trailing \ to escape the new line at the end of a line:

public class Main {
    public static void main(String[] args) {
        String paragraph =
            """
            aaaa bbbb cccc \
            dddd eeee ffff \
            gggg hhhh iiii \
            """;
        System.out.println(paragraph);
    }
}

Prints:

aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii
like image 51
Jorn Vernee Avatar answered Oct 10 '22 14:10

Jorn Vernee


You can use String.lines introduced since Java-11 as:

String output = paragraph.lines().collect(Collectors.joining());

A complimentary and for what's worth it, here is a screenshot from JShell execution of the two different blocks of code:

enter image description here

like image 31
Naman Avatar answered Oct 10 '22 15:10

Naman


Indeed, finding a good answer in Java 13 is not easy (maybe even impossible) but I think Jorn Vernee addressed the concern gracefully in his reply (new escape sequences in Java 14 provide an acceptable solution).

To expand on several listed concerns:

  • Holger mentions "the 'text blocks' feature is not very convincing". I have seen a lot of "ugly" code that does artificial string concatenation, so I think the feature definitely addresses an issue. On the other side, I have to agree that a feature like this leaves a lot of room for abuse (as in "let us put a lot of text in the Java code because now we can nicely do it!" - see the next point).
  • The original question mentions "large paragraphs" which make me advise not using this feature at all, but externalizing that content to resource files.
  • About the clarity and that certain aspects "are not obvious to the reader, so we have a syntax with a non-obvious meaning". That is partly correct, and it takes some reading and experimentation while the available material is not that vast at this moment. I found this: Programmer's Guide To Text Blocks, which provides quite a lot of examples. I also authored some examples, the one on text blocks being here: TextBlocks.java.

(Let's not forget that text blocks are in 'second preview' mode).

like image 22
Chris T Avatar answered Oct 10 '22 16:10

Chris T