Strip indent in Groovy multiline strings

Groovy

Groovy Problem Overview


Unfortunately stripIndent on a multiline string does not work. Side note: My IDE code style preferences allow only space indentation (tabs will be replaced by spaces). But I think this should have no matter.

def s = """ This 
            is
            multiline
"""
println s.stripIndent()

does not print

This
is
multiline

as expected.

The output is with indentation.

This 
               is
               multiline

What is going wrong here?

I use Groovy 2.0.7 with Eclipse Indigo SR2.

The problem seems to disappear with the use of the backslash \ (String continuation character) in first line. But I don't understand why this is a must.

Groovy Solutions


Solution 1 - Groovy

You can use .stripIndent() to remove the indentation on multi-line strings. But you have to keep in mind that, if no amount of indentation is given, it will be automatically determined from the line containing the least number of leading spaces.

Given your string this would be only one white space in front of This which would be removed from every line of your multi-line string.

def s = """ This 
            is
            multiline
"""

To work around this problem you can escape the first line of the multi-line string as shown in the following example to get your expected result:

def s = """\
           This
           is
           multiline
"""

Solution 2 - Groovy

Use .stripMargin() as well (if feasible).

def s = """ This 
            | is
            | multiline
        """
println s.stripMargin().stripIndent()

Solution 3 - Groovy

For anyone else having a similar problem, stefanglase's solution is great but is giving me a MultipleCompilationErrorsException in a Spock test when including a multiline String in an assertion that fails:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Spec expression: 1: unexpected char: '\' @ line 1, column 16.
   myString == '''\ This Is Multiline '''.stripIndent()

My solution to this is:

def myString = '''
  This
  Is
  Multiline
  '''.replaceFirst("\n","").stripIndent()

Now, when the assertion fails, you will see the usual diff indicating what went wrong, rather than the compilation exception.

Solution 4 - Groovy

stripMargin() is to strip leading spaces from lines with margin.

Default margin is |. We can also specify a custom margin.

For example,

def s = """*This 
        *is
            *multiline
"""

println(s.stripMargin("*"))

will result in

This 
is
multiline

The best practice is that we append .trim() in the end to eliminate leading and trailing spaces of the whole string.

For example,

println(s.stripMargin("*").trim())

Solution 5 - Groovy

I have a similar use-case because I'd like to format my SQL query in line. For example the nested query:

String query = '''
    select ${CONTROL_ID} from ${TABLE_NAME} 
        where 
            location_id = ( 
                select id from location where code = ${locationCode} 
            )  
''';

Looks a lot better in Groovy than a Java version with "..."+ TABLE_NAME +"..." as I'm sure you can agree.

This kind of string doesn't work with the .stripIndent() method. Instead I retained the new lines in query (above) for a purpose -- Note no "" at the end of line.

And thus

String query = """
    select ${CONTROL_ID} from ${TABLE_NAME} 
        where 
            location_id = ( 
                select id from location where code = '${locationCode}' 
            )  
""".replaceAll( /\n\s*/, " " );

println  "  Query= ${query};"; 

Give a neatly formatted single line SQL query result:

  Query = select id from controls where  location_id = (  select id from location where code = '003');

Which I find quite helpful and easier to read. I replace with a single space to make sure the SQL stays discrete. The important part is to use the newline(\n) as a de facto start-line or anchor. Fine for mixed TAB-s and SPACE-s.

Of course it also works for the original question too.

Solution 6 - Groovy

Did you intend to use == instead of = ? The errors I get is when using your example. If I change it back to = and use your example without the replaceFirst() it works fine with no errors.

Also you can not use a \ when doing a single line. I can duplicate your exact issue if I use '''\ This Is Multiline '''.stripIndent()

Solution 7 - Groovy

As @stefanglase mentioned I use .stripIndent() combined with .trim():

String mystring = """
     My multiline
          string
     contains blank lines
           at the beginning and the end
"""
print "*${mystring.stripIndent()}*"
print "*${mystring.stripIndent().trim()}*"


>*
>My multiline
>     string
>contains blank lines
>      at the beginning and the end
>*
>*My multiline
>     string
>contains blank lines
>      at the beginning and the end*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCengizView Question on Stackoverflow
Solution 1 - GroovystefanglaseView Answer on Stackoverflow
Solution 2 - GroovydmahapatroView Answer on Stackoverflow
Solution 3 - GroovyJosh FeldmanView Answer on Stackoverflow
Solution 4 - GroovyGraceMengView Answer on Stackoverflow
Solution 5 - GroovywillView Answer on Stackoverflow
Solution 6 - GroovyProtocol-XView Answer on Stackoverflow
Solution 7 - GroovyMaxView Answer on Stackoverflow