strip indent in groovy multiline strings

后端 未结 7 874
名媛妹妹
名媛妹妹 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:59

    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*
    

提交回复
热议问题