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