Groovy基础语法

1.数据类型

1.1 String字符串

  • contains()是否包含某个内容 返回True False

    groovy:000> "devops".contains("ops")
    ===> true
    groovy:000> "devops".contains("a")
    ===> false

  • size() 统计字符串的长度

    groovy:000> "devops".size()
    ===> 6

  • indexOf() 元素的索引

    groovy:000> "devops".indexOf('d')
    ===> 0
    groovy:000> "devops".indexOf('e')
    ===> 1
    groovy:000> "devops".indexOf('v')
    ===> 2
    groovy:000> "devops".indexOf('devops')

  • endsWith() 是否以某个内容结尾 返回 True False

    groovy:000> "devops".endsWith("s")
    ===> true
    groovy:000> "devops".endsWith("a")
    ===> false

  • 字符串的增加和减少

    groovy:000> "dev"+"ops"
    ===> devops
    groovy:000> "devops"-"ops"
    ===> dev

  • split 字符串分割默认是返回列表

    groovy:000> "devops".split()
    ===> [devops]
    groovy:000> "d e v o p s".split()
    ===> [d, e, v, o, p, s]
    groovy:000> "d,e,v,o,p,s".split(",")
    ===> [d, e, v, o, p, s]
    groovy:000> "d,e,v,o,p,s".split()
    ===> [d,e,v,o,p,s]
    groovy:000> "d,e,v,o,p,s".split(",")
    ===> [d, e, v, o, p, s]
    groovy:000>

1.2 list列表

  • 字符串添加

    groovy:000> [1,2,3,4,5]+5
    ===> [1, 2, 3, 4, 5, 5]
    groovy:000> [1,2,3,4,5]<<6
    ===> [1, 2, 3, 4, 5, 6]

  • unique() 去重

    groovy:000> [1,2,3,4,5,5,5,5].unique()
    ===> [1, 2, 3, 4, 5]

  • contains() 包含特定元素
    groovy:000> [1,2,3,4,5,5,5,5].contains(2)
    ===> true
    groovy:000> [1,2,3,4,5,5,5,5].contains(333)
    ===> false

    groovy:000> a = "java,python,nodejs".split(",")
    ===> [java, python, nodejs]
    groovy:000> for (i in a){
    groovy:001>     println(i)
    groovy:002> }
    java
    python
    nodejs
    ===> null

1.3 map

size() map大小
[’key’]  .key get()  获取value
isEmpty() 是否为空
containKey() 是否包含key
containValue() 是否包含指定的value
keySet() 生成key的列表
each{} 遍历map
remove(‘a‘)  删除元素(k-v)

练习

groovy:000> [:]
===> [:]
groovy:000> [1:2]
===> [1:2]
groovy:000> [1:2][1]
===> 2
groovy:000> [1:2,3:4,5:6]
===> [1:2, 3:4, 5:6]
groovy:000> [1:2,3:4,5:6].keySet()
===> [1, 3, 5]
groovy:000> [1:2,3:4,5:6].values()
===> [2, 4, 6]
groovy:000> [1:2,3:4,5:6] + [7:8]
===> [1:2, 3:4, 5:6, 7:8]
groovy:000> [1:2,3:4,5:6] - [7:8]
===> [1:2, 3:4, 5:6]

2.if语句

groovy:000> buildtype = "java"
===> java
groovy:000> if (buildtype == "java"){
groovy:001>     println("this is java project")
groovy:002> }else if (buildtype == "python"){
groovy:003>     println("this is java project")
groovy:004> }else{
groovy:005>     println("this is java project")
groovy:006> }
this is java project
===> null

3.switch语法

switch("${buildType}"){
    case ”maven":
    //xxxx
        break;
    case ”ant":
    //xxxx
        break;
    default:
    //xxxx
}

练习
switch("${buildType}"){
    case 'maven':
        println("This is a maven project !")
        break;
        ;;

    case 'gradle':
        println("This is a gradle projects!")
        break;
        ;;

    default:
        println("Project Type Error!")
        ;;
}

4.for循环

test = [1,2,3]
for ( i in test){
    ///xxxxxx
    break;
}

//代码
langs = ['java','python','groovy']

for ( lang in langs ){
    if (lang == "java"){
        println("lang error in java")
    }else {
        println("lang is ${lang}")
    }
}
posted @ 2021-08-04 10:03  刘新元  阅读(487)  评论(0编辑  收藏  举报