groovy函數、字符串、循環


三重單引號字符串
  '''a triple single quoted string''' 三重單引號字符串是普通的java.lang.String

三重單引號字符串是多行的。您可以跨越行邊界跨越字符串的內容,而無需將字符串拆分為多個部分,而不使用連接或換行轉義字符

def aMultilineString = '''line one
    line two
    line three'''

雙引號字符串

  Double quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.

字符串插值

  除了單引號和三引號字符串之外,任何Groovy表達式都可以在所有字符串文字中進行插值。插值是在對字符串求值時將字符串中的占位符替換為其值的行為。

groovy循環
  定義一個獨立的repeat函數
  方法體就是一個for循環

groovy腳本形式代碼

/**
 * Created by Jxy on 2018/12/19 10:32
 * 定義函數
 * 字符串
 * 字符串插值
 */

println function()
/*
定義函數
 */
def function(){
    "groovy"
}
//字符串插值
def name = 'Guillaume' // a plain string
def greeting = "Hello ${name}"

assert greeting.toString() == 'Hello Guillaume'

def sum = "The sum of 2 and 3 equals ${2 + 3}"
assert sum.toString() == 'The sum of 2 and 3 equals 5'

def person = [name: 'Guillaume', age: 36]
assert "$person.name is $person.age years old" == 'Guillaume is 36 years old'

//GString and String hashCodes
//GStrings and Strings don’t have the same hashCode.
assert "one: ${1}".hashCode() != "one: 1".hashCode()

/*
默認參數值
 */
def repeat(val, repeat=5){
    for(i in 0..<repeat){
        println val
    }
}

repeat("jser",3)//循環三次
repeat("good day")//循環默認值五次

運行結果

groovy
jser
jser
jser
good day
good day
good day
good day
good day

Process finished with exit code 0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM