Groovy的方法小结

// normal strings
deffirstname= 'Kate'
defsurname= "Bush"
assertfirstname* 2== 'KateKate'
// GString
deffullname= "$firstname$surname"
assertfullname== 'Kate Bush'
assertfullname-firstname== ' Bush'
assertfullname.padLeft(10) ==
' Kate Bush'
// indexing (including ranges)
assertfullname[0..3] == firstname
assertfullname[-4..-1] == surname
assertfullname[5, 3..1] == 'Beta'
// more substrings
string = 'hippopotamus'
assertstring -'hippo'-'mus'+ 'to'== 'potato'
assertstring.replace('ppopotam','bisc') == 'hibiscus'
// processing characters
assert'apple'.toList() == ['a', 'p', 'p', 'l', 'e']
//also: 'apple' as String[], 'apple'.split(''), 'apple'.each{}
string = "an apple a day"
assertstring.toList().unique().sort().join() == ' adelnpy'
// reversing chars/words
assert'string'.reverse() == 'gnirts'
string = 'Yoda said, "can you see this?"'
revwords= string.split(' ').toList().reverse().join(' ')
assertrevwords== 'this?" see you "can said, Yoda'
words = ['bob', 'alpha', 'rotator', 'omega', 'reviver']
bigPalindromes= words.findAll{w-> w == w.reverse() && w.size() > 5}
assertbigPalindromes== ['rotator', 'reviver']


你可能感兴趣的:(apple,groovy)