[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
groovy有两种字符串:String和GString
GString即支持占位插值符${}的字符串。
<>1.四类字符串
1.单引号字符串
def x = "123" def str = 'abc' //不可变字符串,不支持占位插值符$ def str1 = 'abc${x}'
//无法解析${x} 结果为abc${x}
2.双引号字符串
def x = "123" def str = "abc${x}" //可变字符串,支持占位插值符$ 结果为abc123,{}可以省略
3.三个单引号字符串
def x = "123" def str = '''abc${x}''' //可以多行输入,不支持占位插值符$,结果为abc${x} def str1 =
''' hello world ''' //结果为 hello world
4.三个双引号字符串
def x = "123" def str = """abc $x""" //可以多行输入,支持占位插值符$,结果为abc 123
总结:
只有双引号或者三个双引号的字符串才能成为GString,在${ }之间可以写任意代码。
要注意的是:
$与要替换的变量之间不能用空格和其它符号。
def x = "123" def str = "$ {x}" //会报错 def str1 = "$ x" //会报错 def str2 = "$(x)"
//会报错
当替换的变量要调用函数时不能省略{ }
def x = 123 def str = "$x.toString()" //会报错 def str = "${x.toString()}" //正确写法
<>2.字符串方法:split()/tokenize()
基本和java的一样,这里主要介绍一下split和tokenize函数的不同。两个函数都是根据给出的字符/字符串切割字符串。不同之处有三个。
2.1返回值类型不同
def str = "hello,world" println str.split().class //返回值为字符串数组 println str.
tokenize().class //返回值为list
2.2 split会保留切割后产生的空格,tokenize自动去除空格
def str = "hello,world" println str.split("l") //[he,,o,wor,d] println str.
tokenize("l") //[he,o,wor,d]
2.3 split用给出的字符串整体切割,tokenize用给出的字符串的字符元素切割
def str = "hello,world" println str.split("lo") //用"lo"切割,[hel, ,world]
println str.tokenize("lo") //用"l","o"切割,[he, ,w, r, d] 注意:此处",w"是一个整体,不是连续的两个逗号
println str.tokenize("el") //用"e","l"切割,[h, o ,wor, d]