groovy-range的使用

2017-01-10 20:06
def result = (0..10).contains(5)
println("(0..10).contains(5)=$result") //(0..10).contains(5)=true

result = (0..<9).contains(10)
println("(0..<9).contains(10)=$result") //(0..<9).contains(10)=false

result = new IntRange(0, 10).contains(5)
println(" IntRange(0, 10).contains(5)=$result") // IntRange(0, 10).contains(5)=true

result = ("a".."d").contains("c")
println("a..d.contains(c)=$result") //a..d.contains(c)=true

result = ""
for (item in 0..9) {
    result += item
}
println(result) //0123456789

result = ""
(9..<5).each { item ->
    result += item
}
println(result) //9876

// 结合switch的用法
def age = 30
switch (age){
    case 16..20: println("boy");break
    case 20..10: println("man");break
    default:println("girl")
}