美文网首页
Groovy基础

Groovy基础

作者: SHAN某人 | 来源:发表于2017-10-17 15:54 被阅读30次

2.字符串和正则表达式
2.1 String字符串
使用引号将字符串括起来就可以构造一个String字符串,Groovy提供了多种表示String字符串的方法,比如可以用('')或("")或(""")来包含.使用" " "来包含字符串可以
跨多行.下面的几个String的例子


'' // 空字符串
'test' // 按字面显示的字符串
'I am a "man" !' // 单引号内部包含双引号的字符串
"I am a 'man'! " // 双引号内部包含单引号的字符串
""" // 可以跨行显示的三个双引号闭合的字符串
Hello , my
name is pwy
""
def age =25
'My age is ${age}' // 返回值 My age is ${age} , 单引号闭合的字符串不能动态显示变量值
"My age is ${age}" // 返回值 My age is 25 , 双引号闭合的字符串可以动态显示${}格式的变量值
""" My age is ${age} """ // 返回值 My age is 25 , 三双引号闭合的字符串可以动态显示变量值,且可以跨行显示
''' My age is ${age} ''' // 返回值 My age is ${age} ,三单引号闭合的字符串不能动态显示变量值,但是可以跨行显示


2.2 String的索引和范围
因为String字符串是一串有序列的字符,因此我们通过一个字符在String字符串的位置来访问它,这个位置就是index索引。注意索引可以指单个字符也可以指一个字 符串的子集.String的索引从0开始到它的长度length-1为止.Groovy可以支持使用负值索引表示从String的末尾倒数.我们也可以用"范围"来表示一个String的子集。


def hello = "hello world!"
hello[0] // h 第一个字符
hello[5] // w 第六个字符
hello[1..2] // el 范围2-3的字符,包括边界3
hello[1..<3] // el 范围2-3的字符,不包括边界4
hello[4..2] // oll 反向范围4-2的字符
hello[4,1,6] // oew 单个位置的字符


2.3 基本操作

def hello = "hello world!"
'hello' + ' world' // hello world
'hello'*3 // hellohellohello
hello - 'o world!' // hell
hello.size() // 12
hello.length() // 12
hello.count('o') // 2
hello.contains('ell') // true


String字符串值是不会被更改的, hello - ' o world' 并没有改变hello变量指向的字符串'hello world'的值,它将重新生成一个新的字符串(和JAVA一样)
2.4 String操作方法


'Hello'.compareToIgnoreCase('hello') // 0
'Hello'.concat('world') // Hello world
'Hello'.endsWith('lo') // true
'Hello'.equalsIgnoreCase('hello') // true
'Hello'.indexOf('lo') // 3
'Hello world'.indexOf('o', 6) // 7
'Hello'.matches('Hello') // true
'Hello'.matches('He') // false
'Hello'.replaceAll('l', 'L') // HeLLo
'Hello world'.split('l') // 'He', 'o wor', 'd'
'Hello'.substring(1) // ello
'Hello'.substring(1, 4) // ell
'Hello'.toUpperCase() // HELLO
def message = 'Hello'
message.center(11) // Hello
message.center(3) // Hello
message.center(11, '#') // ###Hello###
message.getAt(0) // H
message.getAt(0..<3) // Hel
message.getAt([0, 2, 4]) // Hlo
message.leftShift('world') // Hello world
message << 'world' // Hello world
message.minus('ell') // Ho
message – 'ell' // Ho
message.padLeft(4) // Hello
message.padLeft(11) // Hello
message.padLeft(11, '#') // ######Hello
message.padRight(4) // Hello
message.padRight(11) // Hello
message.padRight(11, '#') // Hello######
message.plus('world') // Hello world
message + 'world' // Hello world
message.replaceAll('[a-z]') { ch -> // HELLO
ch.toUpperCase() }
message.reverse() // olleH
message.toList() // ['H', 'e', 'l', 'l', 'o']
def message = 'Hello world'
message.tokenize() // ['Hello', 'world']
message.tokenize('l') // ['He', 'o wor', 'd']
2.5 String的比较
Groovy支持比较String的方法,我们比较两个字符串对象 str1 == str2 实际上是使用的是str1.equals(str2)的一种简便方式,同样 str1 <=> str2 表示
str1.compareTo(str2).


'pwy' <=> 'pwy' // 0 相等
'pwy' <=> 'pwyisme' // -1 小于
'pwy' <=> 'Pwy' // 1 大于
'pwy'.compareTo('Pwy') // >0
2.6 正则表达式
正则表达式是用来在文本中查找子字符串的匹配模式,String类中有一些方法允许我们用正则表达式来操作String字符串. matches方法,如果接收的String
字符串匹配到了给定的正则表达式参数则返回true,比如"abc".matches("abc") 返回true,而"abc".matches("bc")返回false.replaceAll方法,使用闭包指定
的替换值来替换所有在String字符串中和正则表达式参数匹配的值.Groovy使用~"regex" 来表示正则表达式,比如: def regex = ~"pwy"
当Groovy的运算符=~作为一个表达式返回的结果出现在if 或while语句中时,左边的运算域会和右边的运算域进行正则匹配.比如:


'handsomepwy' =~ 'pwy' // true
!('handsomepwy'=~'cuiwenjing') // true
'handsomepwy' =~ regex // true
'handsomepwy' ==~ 'pwy' // false


而运算符 ==~表示精确匹配,所以 'handsomepwy' ==~ 'pwy'返回false.
(* 注意 def regex = ~"haha" 表示创建一个正则表达式~"haha",然后赋值给变量regex , 而之后的 'handsomepwy' =~ regex 中的 =~是一个运算符,表示将字符串
'handsomepwy'和 表达式 regex 进行正则匹配,其中regex是作为条件匹配的表达式,而'handsomepwy'是被匹配的字符串.)
在正则表达式中,两个特殊位置的字符用来表示一行的开始(^)和结束($)


def ask = 'pwy is a handsome man'
ask =~ '^pwy' // true
ask =~ '$man' // true


正则表达中还包含计数器,'+'表示1次或多次, '*'表示0次或多次,'?'表示0次或1次,'{}'用来匹配一个指定数量次数的字符

'aaaaab' =~ 'ab' // true
'b' =~ 'a
b' // true
'aaacd' =~ 'ac?d' // true
'aaad' =~ 'a
c?d' // true
'aaaaab' =~ 'a{5}b' // true
!('aab' =~ 'a{5}b') // true


正则表达式中的'.' 叫做通配符,用来表示任何字符,如果要表示字符'.'本身,则需要使用\.转义

ask =~ '.man' // true
'3.14' =~ '3.14' // true
'3X14' =~ '3.14' // true
'3.14' =~ '3\.14' // true
!('3X14' =~ '3\.14') // true


在正则表达式中使用反斜杠''要注意,通常反斜杠用来表示转义,所以'\'表示它本身,在正则表达式中一个字符反斜杠用'\\'来表示,要使用四个.
正则表达式还包含字符集合,通常使用[]来闭合,中间的范围用-来分隔,用^来表示取反,即获取除了[]中范围以外的集合 比如:


ask =~ '[pz]wy' // true
!(ask =~ '[ak]wy') // true
!(ask =~ [^pz]pwy) // true


相关文章

网友评论

      本文标题:Groovy基础

      本文链接:https://www.haomeiwen.com/subject/gapduxtx.html