groovy字符串操作

1.字符串的拼接类似python 就是使用 “+”

class Test{
static void main(String [] args){
	String a = "hello";
	String b = " world \n";
	def c  = a+b;
	println(c);
}
}

字符串索引

class Test{
static void main(String [] args){
	String a = "hello";
	String b = " world";
	def c  = a+b;
	println(c[-1]+"\n");
	println(c[1..4]+"\n");
	println(c[4..2]+"\n");
	}
}

字符串乘法

class Test{
static void main(String [] args){
	String a = "hello";
	println(a*2);
	println(a.length());
	}
}

打印结果
hellohello
5
string.center(count)
将一个字符串变为

class Example { 
   static void main(String[] args) { 
      String a = "HelloWorld"; 
      println(a.center(30)); 
      println(a.center(30).length());
   } 
}

比较字符串忽略大小写
compareToIgnoreCase(String str)

class WuYong {
	static void main(String [] args){
		String a = "abc";
		String b = "aCc";
		a.compareToIgnoreCase(b);
	}
}

结果是-1因为c>b且忽略大小写

concat函数尾部追加

class Example {
   static void main(String[] args) {
      String s = "Hello ";
      s = s.concat("Yong");
      println(s)
   } 
}

匹配打印字符串
匹配点就是单个字符所以就都匹配到了

class Example {
   static void main(String[] args) {
      String s = "HelloWorld";   
      s.eachMatch(".") {
         ch -> println ch
      }
   }
}

endsWith(“ld”) 以什么结尾的

class Test{
   static void main(String[] args) {
      String s = "HelloWorld";
		
      println(s.endsWith("ld"));
      println(s.endsWith("lo"));
      println("Hello".endsWith("lo"));
   } 
} 

reverse 字符串翻转

class Test{
   static void main(String[] args) {
      String s = "HelloWorld";
      println(s.reverse());
   } 
}

dlroWolleH

class Test{
   static void main(String[] args) {
      String s = "HelloWorld";
      println(s.reverse());
   } 
}

根据索引来获取字符串的元素

class Test{
	static void main(String [] args){
		String a = "hello world";
		println(a.getAt(4));
	}
}

打印结果
o

indexOf

class Test{
	static void main(String [] args){
		String a = "hello world";
		println(a.indexOf("o"));
	}
}

4
从指定索引开始找

class Test{
	static void main(String [] args){
		String a = "hello world";
		println(a.indexOf("o",5));
	}
}

值为7 因为从索引5开始查找。

matches() 匹配正则表达式,匹配到了返回true

class Test{
	static void main(String[] args){
		String a = "test groovy";
		println(a.matches("test(.*)"))
	}
}

打印结果
true

删除字符串中的部分字符串

class Test {
	static void main(String [] args){
		String b  ="free of charge";//福瑞ofcharge
		def c = b.minus("free");
		println(c);
	}

}

打印结果是of charge 删除了free 就需要你pay money啦。

next 最后一个字母加一

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

d 后面是e
所以打印结果是Hello Worle

previous前一个

class Test { 
   static void main(String[] args) {
      String a = "Hello"; 
      println(a.previous());
   }
}

Helln

padLeft 在字符串的左边填充值
padLeft(16,"*") 字符总长度

class Test{
	static void main(String[] args){
		String a = "hello groovy";
		println(a.padLeft(16));
		println(a.padLeft(16,"*"));
		println(a.padRight(16,"*"));
	}
}
hello groovy

hello groovy
hello groovy

plus

class Test{ 
   static void main(String[] args) { 
      String a = "Hello";
		
      println(a.plus("World")); 
      println(a.plus("World Again")); 
   } 
}

HelloWorld
HelloWorld Again

replaceAll替换所有的

class Test {
	static void main(String [] args){
		String a = "hello world";
		println(a.replaceAll("hello","good"));
	}
}

good world

split 字符串分割

class Test{
	static void main(String [] args){
		String a = "hello blue sky";
		String [] ar;
		ar =  a.split(" ");
		println(ar)
	}
}

[hello, blue, sky]

String substring(int beginIndex, int endIndex)

class Example { 
   static void main(String[] args) { 
      String a = "HelloWorld"; 
      println(a.substring(4)); 
      println(a.substring(4,8));
   }
}

oWorld
oWor

class Example { 
   static void main(String[] args) { 
      String a = "HelloWorld"; 
      println(a.toUpperCase()); 
      println(a.toLowerCase());
   } 
}

HELLOWORLD
helloworld

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值