groovy风格字符串截取、赋值、拼接

 

1、截取字符串

def text1 = "My last character will be removed soon"    

println text1[0..-2] // My last character will be removed soo   

  

def text2 = "My first word will be removed soon";    

println text2[3..-1] // first word will be removed soon   

  

def text3 = "noos em daer lliw uoy ,tneitap eB"    

println text3[-1..0] // Be patient, you will read me soon


def text1 = "Sorry, I need to separate from you"    
println text1 - " you" // Sorry, I need to separate from    
 
def text2 = "Minus string usage"    
println text2.minus(" usage") // Minus string

2、将字符串赋值给数组

1)方式1

def b=字符串
String a=b[1..-2]
    String[] str;
    str = a.split(', ');
    for(int i in str) { 
        def c="${i}"
        echo "${c}"
	}

2)方式2

def b=字符串
String a=b[1..-2]
String[] str;
str = a.split(', ');
for( String values : str )
println(values);

3、字符串拼接

语法

字符串的串联可以通过简单的\'+\'运算符来完成。

String+String

参数 - 参数将为2个字符串作为& plus;的左右操作数。 运算符。

返回值 - 返回值是一个字符串

class Example {
   static void main(String[] args) {
      String a = "Hello";
      String b = "World";
		
      println("Hello" + "World");
      println(a + b);
   }
}

运行结果

 

你可能感兴趣的:(groovy)