个性化阅读
专注于IT技术分析

groovy的字符串

点击下载

本文概述

字符串是字符序列。通常,字符串是一种数据类型,并实现为存储字符序列的字节数组。在Groovy中,String类在java.lang.String对象以及groovy.lang.GString中都可用,在某些编程语言中将其称为插值字符串。

注意:在常规中,可以使用运算符将​​所有字符串连接在一起

单引号字符串

在常规中,单引号字符串是用单引号引起来的一系列字符。

句法:

'________String________'

范例1:

package com.app
class GroovyStringExample1 {
static void main(args)
{
		String s1 = 'srcmini'
		println s1
		println 'This is tutorial on Groovy at ' + s1
}
}

输出:


双引号字符串

在groovy中,双引号字符串是用双引号引起来的字符序列。

句法:

"________String________"

范例2:

package com.app
class GroovyStringExample2 {
static void main(args)
{
		String s1 = "srcmini"
		println s1
		println "This is tutorial on Groovy at " + s1
}
}

输出:

在Groovy中,我们也可以使用$ {Variable _name}和$ Variable_name而不是使用”运算符。使用$ Variable_name称为插值。仅当在双引号内定义了字符串时才可以使用它。

范例3:

package com.app
class GroovyStringExample3 {
static void main(args)
{
		String s1 = "srcmini"
		println "This is tutorial on Groovy at ${s1} "
		println "This is tutorial on Groovy at $s1 "
}
}

输出:


三重单引号字符串

在groovy中,三重单引号字符串是用三重单引号括起来的字符序列。

句法:

'''________String________'''

范例4:

package com.app
class GroovyStringExample4 {
static void main(args)
{
		String s1 = '''This is groovy tutorial and we are learning string'''
		println s1
}
}

输出:

在Groovy中,三重单引号的字符串通常用于单个变量中的多行。

范例5:

package com.app
class GroovyStringExample5 {
static void main(args)
{
String s1 = '''This is line 1
This is line 2
This is line 3
This is line 4
This is line 5'''
println s1
}}

输出:


三重双引号字符串

在groovy中,三重双引号字符串是用三重双引号括起来的字符序列。

句法:

"""________String________"""

范例6:

package com.app
class GroovyStringExample6 {
static void main(args)
{
		String s1 = """This is groovy tutorial and we are learning string"""
		println s1
}
}

输出:

在Groovy中,三重双引号字符串也用于单个变量中的多行。

范例7:

package com.app
class GroovyStringExample7 {
static void main(args)
{
String s1 = """This is line 1
This is line 2
This is line 3
This is line 4
This is line 5"""
		println s1
}
}

输出:

在三重双引号字符串中,我们也可以使用$ {Variable _name}和$ Variable_name而不是使用”运算符。使用$ Variable_name称为插值。

范例8:

package com.app
class GroovyStringExample8 {
static void main(args)
{
String s1 = """This is line 1
This is line 2
This is line 3
This is line 4
This is line 5"""
println """Hello $s1"""
println """Hey $s1"""
}
}

输出:


斜线

在groovy中,斜杠字符串是一个包含在/正斜杠中的字符序列。斜线字符串可用于定义不需要转义反斜杠的正则表达式和模式。

句法:

/________String________/

范例9:

package com.app
class GroovyStringExample9 {
static void main(args)
{
String s1 = /This is groovy tutorial and we are learning string/
println s1
}
}

输出:

在Groovy中,斜线字符串也用于单个变量中的多行。

范例10:

package com.app
class GroovyStringExample11 {
static void main(args)
{
String s1 = /This is line 1
This is line 2
This is line 3
This is line 4
This is line 5/
		println s1
}}

输出:

在斜线字符串中,我们也可以使用$ {Variable _name}和$ Variable_name而不是使用”运算符。使用$ Variable_name称为插值。

示例11:

package com.app
class GroovyStringExample11 {
static void main(args)
{
String s1 = /This is line 1
This is line 2
This is line 3
This is line 4
This is line 5/
println """Hello ${s1}"""
println """Hey $s1"""
}}

输出:


美元斜线字符串

在groovy中,美元斜杠字符串是一个字符序列,包含在$ /美元和一个正斜杠中。美元斜杠字符串可用于定义不需要转义反斜杠的正则表达式和模式。

句法:

$/________String________/$

示例12:

package com.app
class GroovyStringExample9 {
static void main(args)
{
String s1 = $/This is groovy tutorial and we are learning string /$
println s1
}
}

输出:

在Groovy中,美元斜线字符串也用于单个变量中的多行。

示例13:

package com.app
class GroovyStringExample13 {
static void main(args)
{
String s1 = $/This is line 1
This is line 2
This is line 3
This is line 4
This is line 5/$
		println s1
}
}

输出:

在美元斜线字符串中,我们也可以使用$ {Variable _name}和$ Variable_name而不是使用”运算符。使用$ Variable_name称为插值。

示例11:

package com.app
class GroovyStringExample11 {
static void main(args)
{
String s1 = $/This is line 1
This is line 2
This is line 3
This is line 4
This is line 5/$
println """Hello ${s1}"""
println """Hey $s1"""
}}

输出:


赞(1)
未经允许不得转载:srcmini » groovy的字符串

评论 抢沙发

评论前必须登录!