当前位置:网站首页>[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
边栏推荐
- Azkaban安装部署
- Kubernetes -- cluster expansion principle
- Six stone programming: advantages of automated testing
- Multi person online anonymous chat room / private chat room source code / support the creation of multiple chat rooms at the same time
- Daily question 2 12
- Character painting, I use characters to draw a Bing Dwen Dwen
- Design and implementation of community hospital information system
- 单项框 复选框
- Spark SQL learning bullet 2
- Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics
猜你喜欢
SPI and IIC communication protocol
SQL performance optimization skills
El select, El option drop-down selection box
Zabbix
Pat grade a 1119 pre- and post order traversals (30 points)
Avoid material "minefields"! Play with super high conversion rate
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Utilisation simple de devtools
Apache Web page security optimization
C file in keil cannot be compiled
随机推荐
Class inheritance in C #
el-select,el-option下拉选择框
有个疑问 flink sql cdc 的话可以设置并行度么, 并行度大于1会有顺序问题吧?
2021 Li Hongyi machine learning (2): pytorch
平台入驻与独立部署优缺点对比
Share the newly released web application development framework based on blazor Technology
Good documentation
Spark SQL learning bullet 2
Asp+access campus network goods trading platform
SQL injection exercise -- sqli Labs
Apache build web host
Une question est de savoir si Flink SQL CDC peut définir le parallélisme. Si le parallélisme est supérieur à 1, il y aura un problème d'ordre?
The latest blind box mall, which has been repaired very popular these days, has complete open source operation source code
Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords
Sqoop command
Linux Installation redis
There is a question about whether the parallelism can be set for Flink SQL CDC. If the parallelism is greater than 1, will there be a sequence problem?
What is the most effective way to convert int to string- What is the most efficient way to convert an int to a String?
Share the newly released web application development framework based on blazor Technology
Cette ADB MySQL prend - elle en charge SQL Server?