当前位置:网站首页>[groovy] string (string splicing | multi line string)

[groovy] string (string splicing | multi line string)

2022-07-05 03:12:00 Programmer community

List of articles

  • One 、 String type variables
  • Two 、 Multiline string
  • 3、 ... and 、 Complete code example

One 、 String type variables


In a double quoted string , Use ${ Variable name } String splicing ,

  • ${ Variable name } The value of the symbol , Is in Value when defining ;
        def s1 = "Hello"        def s3 = "${s1} World!!!"        s1 = "Hello Groovy" //  Modify the spliced function value         //  The value printed is  "Hello World!!!"        println s3
  • ${ -> Variable name } The value of the symbol , Is in Access value , Not when defined , If before the visit , Revised Variable name The corresponding variable value , Then the last value is the modified value ; This is the rule of closure ;
        def s1 = "Hello"        def s4 = "${->s1} World!!!"        s1 = "Hello Groovy" //  Modify the spliced function value         //  The value printed is  "Hello Groovy World!!!"        println s4

stay Single quotation marks in , Cannot perform string connection operation ;

        //  String splicing is not allowed in single quotation marks         def s5 = '${s1} World!!!'        //  The value printed is  "${s1} World!!!"        println s5

String splicing code example :

        //  character string         def s1 = "Hello"        //  String splicing         //  Be careful  ,  String splicing can only be carried out in double quotation marks  ,  Not in single quotes         // ${ Variable name }  The value of the symbol  ,  Is in   Value when defining  ;        def s3 = "${s1} World!!!"        // ${-> Variable name }  The value of the symbol  ,  Is in   Access value  ,  Not when defined  ,        //  If before the visit  ,  Revised   Variable name   The corresponding variable value  ,  Then the last value is the modified value  ;        //  This is the rule of closure  ;        def s4 = "${->s1} World!!!"        //  Modify the spliced function value         s1 = "Hello Groovy"        //  Print respectively  s3  String content and type  ,  Modifying the spliced function value does not affect the final string value         println s3        println s3.class        //  Print respectively  s4  String content and type         //  Modify the spliced function value   It's affecting   Final string value         println s4        println s4.class        //  String splicing is not allowed in single quotation marks         def s5 = '${s1} World!!!'        //  Print respectively  s4  String content and type         println s5        println s5.class

Execution results :

Hello World!!!class org.codehaus.groovy.runtime.GStringImplHello Groovy World!!!class org.codehaus.groovy.runtime.GStringImpl${ 
    s1} World!!!class java.lang.String

Two 、 Multiline string


stay In a normal string , Use \n Symbol , Represents line feed operation ,

def m1 = "Hello\nWorld"

representative

HelloWorld

character string ;

stay

6

6

6 Between two double quotes , You can write multiple lines of text directly , Multiline text There are... On the left and right

3

3

3 Double quotes ;

        def m2 = """HelloWorld"""

representative

HelloWorld

character string ;

Code example :

        //  Multiline string output         def m1 = "Hello\nWorld"        def m2 = """HelloWorld"""        //  Print multiline strings         println m1        println m2

Execution results :

HelloWorldHelloWorld

3、 ... and 、 Complete code example


Complete code example :

class Test { 
        static void main(args) { 
            //  character string         def s1 = "Hello"        //  String splicing         //  Be careful  ,  String splicing can only be carried out in double quotation marks  ,  Not in single quotes         // ${ Variable name }  The value of the symbol  ,  Is in   Value when defining  ;        def s3 = "${s1} World!!!"        // ${-> Variable name }  The value of the symbol  ,  Is in   Access value  ,  Not when defined  ,        //  If before the visit  ,  Revised   Variable name   The corresponding variable value  ,  Then the last value is the modified value  ;        //  This is the rule of closure  ;        def s4 = "${->s1} World!!!"        //  Modify the spliced function value         s1 = "Hello Groovy"        //  Print respectively  s3  String content and type  ,  Modifying the spliced function value does not affect the final string value         println s3        println s3.class        //  Print respectively  s4  String content and type         //  Modify the spliced function value   It's affecting   Final string value         println s4        println s4.class        //  String splicing is not allowed in single quotation marks         def s5 = '${s1} World!!!'        //  Print respectively  s4  String content and type         println s5        println s5.class        //  Multiline string output         def m1 = "Hello\nWorld"        def m2 = """HelloWorld"""        //  Print multiline strings         println m1        println m2    }}

Execution results :

Hello World!!!class org.codehaus.groovy.runtime.GStringImplHello Groovy World!!!class org.codehaus.groovy.runtime.GStringImpl${ 
    s1} World!!!class java.lang.StringHelloWorldHelloWorld

 Insert picture description here

原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140806222283.html