Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy multiline string keep new line and indentation

If we have a multiline string in groovy, like this:

def multilineString = """
Lorem ipsum dolor sit amet,
        consectetur adipiscing elit,
        sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
"""

then using a simple println multilineString; will give an output for each line on a new line. Like this

Lorem ipsum dolor sit amet,
            consectetur adipiscing elit,
            sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.

I would like groovy to OUTPUT in the following format, keeping all the new line characters '\n', potentially tab characters '\t' and the indentation.

Desired output is a string on a single line that looks as follows:

Lorem ipsum dolor sit amet,\n consectetur adipiscing elit,\n sed do eiusmod tempor incididunt\nut labore et dolore magna aliqua.

Any help or suggestions would be appreciated. Completely new to groovy scripting.

like image 692
hjuk12 Avatar asked Dec 06 '25 07:12

hjuk12


1 Answers

You could use an approach like println multilineString.replaceAll('\n', '\\\\n')

like image 186
Jeff Scott Brown Avatar answered Dec 08 '25 22:12

Jeff Scott Brown