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);
   }
}

运行结果

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值