Groovy 学习笔记 (一)
1. 关于字符串:
简单字符串可以用单引号和双引号, 但如果使用GString, 则必须使用双引号. 比如 “$foo, hello world”
多行字符串则可以使用 “”" (3个双引号), 例如:
def text = “”"\
hello there ${name}
how are you today?
“”"
如果对这个text进行输出,会发现输出是按原样式输出的,即换行符也输出. 这在处理类似 HTML 代码时时特别有用的.
另外, 可以使用 /…/ 来定义字符串, 如: def basename = /[Strings and GString^\/]+$/
在这种情况下, 只需对 / 进行转义: \/; 但如下定义是不合法的:def x = //, 可以这么写: def x = /${}/
2.字符串操作:
a). 字符串加减操作
def string = ‘hippopotamus’
print string – ‘hippo’ – ‘mus’ + ‘to’ == ‘potato’ // 果然强悍!!!
b).字符串转化为字符数组
‘apple’.toList()
def string = “an apple a day”
assert string.toList().unique().sort().join() == ‘ adelnpy’
c).字符串反转
assert’string’.reverse() == ‘gnirts’
def string = ‘Yoda said, “can you see this?”‘
def revwords= string.split(‘ ‘).toList().reverse().join(‘ ‘)
assert revwords== ‘this?” see you “can said, Yoda’
d).字符串查找
def words = ['bob', 'alpha', 'rotator', 'omega', 'reviver']
def bigPalindromes = words.findAll {w -> w == w.reverse() && w.size() > 5}
assert bigPalindromes == ['rotator', 'reviver']
3. 数字
a). 一切皆为对象, 数字也不例外
b). 对于非整型数字, 使用 BigDecimal 作为默认类型
4. 日期
可以直接对日期操作: def date = new Date() + 1
还可以:
use(TimeCategory) {
println new Date() + 1.hour + 3.weeks -2.days
}
其中 TimeCategory 是 org.codehaus.groovy.runtime.TimeCategory
5.
Categorized in: Scripts · Tagged with: grails, Groovy


(
on 2009/04/29 at 15:08
· Permalink
博主您好,介意交换个链接不?交个朋友方便以后访问~~贵站链接已经做好,请审核!
网站名称:时尚起义
网站地址:http://www.lan07.cn/
on 2009/04/29 at 23:05
· Permalink
这是为什么呢? 你我两个站点的定位完全不同, 交换链接意义不大