strip indent in groovy multiline strings

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

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

    def s = """ This 
                | is
                | multiline
            """
    println s.stripMargin().stripIndent()
    
    0 讨论(0)
  • 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
    """
    
    0 讨论(0)
  • 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*
    
    0 讨论(0)
  • 2020-12-08 14:07

    Did you intend to use == instead of = ? The only 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()

    0 讨论(0)
  • 2020-12-08 14:09

    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.

    0 讨论(0)
  • 2020-12-08 14:13

    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.

    0 讨论(0)
提交回复
热议问题