GROOVY 字符串

class Example{
    static void  main(String[] args) {
        String sample = "hello world";
        println(sample[4]);
        println(sample[-1]);
        // 包头不包尾
        println(sample[1..2]);
        // 字符串的连接
        String a = "hello";
        String b = " world!";
        println(a+b);
        // 字符串重复
        println(a*3);
    }
}
class Example{
    static void  main(String[] args) {
        String sample = "hello world";
        // 字符串长度 length()
        println(sample.length());

        String a = "hello";
        // 
        println(a.center(10));

        String str1 = "Hello World";
        String str2 = "HELLO WORLD";
        // 按字符顺序比较,忽略大小写,返回值为两个字符串长度差
        System.out.println(str1.compareToIgnoreCase( str2 ));
        // 两个字符串进行比较,并忽略大小写。返回值为true
        println(str1.equalsIgnoreCase(str2));

        String b = "a";
        // 将指定的字符串连接到此字符串的结尾
        println(b.concat("b"));

        String s = "helloworld";
        s.eachMatch("."){
            ch -> println ch;
        }

        String str3 = "a.tar";
        // 判断字符串以什么结尾,成功返回true
        println(str3.endsWith(".tar"));
    }
}
class Example{
    static void  main(String[] args) {
        String a = "hello,world";
        // 返回索引在的字符
        println(a.getAt(2));

        // 返回此字符在当前字符串首次出现的下标
        println(a.indexOf("o"));

        println(a.matches("hello"));
        println(a.matches("hello.*"));
        // 减去字符串
        println(a.minus("hello"));
        // 添加给定字符串中的最后一个字符
        println(a.next());
        // 在字符左侧添加字符,不足部分用*号填充
        println(a.padLeft(14,"*"));
        // 在字符右侧添加字符,不足部分用*号填充
        println(a.padRight(14,"*"))

        String b = "hello";
        // 添加字符
        println(b.plus(",April"));

        println(b.previous());

        String d = "hello world hello";
        // 替换
        println(d.replaceAll("hello","baby"));

        def lst = [12,13,14,15];
        // 反转列表
        println(lst.reverse());

        String str1 = "hello-world";
        String[] str;
        // 以-为分隔符
        str = str1.split("-");
        for(String value:str){
            println(value);
        }

        // 从4以后的字符串
        println(a.substring(4));
        // 4-8 之间的字符,包头不包尾
        println(a.substring(4,8));

        // 将字符串转换成大写
        println(a.toUpperCase());
        // 将字符串转换成小写
        println(a.toLowerCase());
    }
}

 

posted @ 2025-07-14 15:39  山村放羊娃  阅读(5)  评论(0)    收藏  举报