strip indent in groovy multiline strings

后端 未结 7 880
名媛妹妹
名媛妹妹 2020-12-08 13:49

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

7条回答
  •  独厮守ぢ
    2020-12-08 13:53

    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
    """
    

提交回复
热议问题