当前位置:网站首页>[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
边栏推荐
- SFTP cannot connect to the server # yyds dry goods inventory #
- IPv6 experiment
- Azkaban overview
- Azkaban installation and deployment
- Structure of ViewModel
- Tencent cloud, realize image upload
- El select, El option drop-down selection box
- 問下,這個ADB mysql支持sqlserver嗎?
- Basic authorization command for Curl
- 2.常见的请求方法
猜你喜欢
8. Commodity management - commodity classification
Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics
Leetcode92. reverse linked list II
1.五层网络模型
Azkaban overview
看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事
Zabbix
2021 Li Hongyi machine learning (1): basic concepts
Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
About MySQL database connection exceptions
随机推荐
Talk about the SQL server version of DTM sub transaction barrier function
The latest blind box mall, which has been repaired very popular these days, has complete open source operation source code
LeetCode --- 1071. Great common divisor of strings problem solving Report
Leetcode42. connect rainwater
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
Leetcode92. reverse linked list II
Pdf things
Apache build web host
Sqoop command
Yyds dry goods inventory embedded matrix
Accuracy problem and solution of BigDecimal
Performance of calling delegates vs methods
SQL performance optimization skills
51 independent key basic experiment
Scientific research: are women better than men?
Why is this an undefined behavior- Why is this an undefined behavior?
Pat class a 1160 forever (class B 1104 forever)
This + closure + scope interview question
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
Utilisation simple de devtools