当前位置:网站首页>[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
边栏推荐
- When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
- Is there any way to change the height of the uinavigationbar in the storyboard without using the UINavigationController?
- Design of KTV intelligent dimming system based on MCU
- IPv6 experiment
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
- 端口,域名,协议。
- SQL performance optimization skills
- 单项框 复选框
- Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
- Ubantu disk expansion (VMware)
猜你喜欢
Avoid material "minefields"! Play with super high conversion rate
51 independent key basic experiment
Master Fur
Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
[200 opencv routines] 99 Modified alpha mean filter
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Azkaban overview
Azkaban安装部署
Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time
Accuracy problem and solution of BigDecimal
随机推荐
IPv6 experiment
Single box check box
【微服务|SCG】Filters的33种用法
Pat class a 1162 postfix expression
問下,這個ADB mysql支持sqlserver嗎?
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
Usage scenarios and solutions of ledger sharing
Asp+access campus network goods trading platform
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
2021 Li Hongyi machine learning (3): what if neural network training fails
Sqoop命令
Devtools的簡單使用
Design and practice of kubernetes cluster and application monitoring scheme
Dart series: collection of best practices
Last words record
Pat class a 1160 forever (class B 1104 forever)
SFTP cannot connect to the server # yyds dry goods inventory #
The database and recharge are gone
单项框 复选框
Character painting, I use characters to draw a Bing Dwen Dwen