Reputation: 4867
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.
Upvotes: 65
Views: 58095
Reputation: 31
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()
Upvotes: 2
Reputation: 1099
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())
Upvotes: 9
Reputation: 529
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*
Upvotes: 3
Reputation: 50245
Use .stripMargin() as well (if feasible).
def s = """ This
| is
| multiline
"""
println s.stripMargin().stripIndent()
Upvotes: 31
Reputation: 547
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.
Upvotes: 7
Reputation: 5061
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.
Upvotes: 4
Reputation: 10402
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
"""
Upvotes: 84