string中常用的方法,我以代码的形式,来说明这些常用的方法。

 @test
    public void test1(){
        //1.返回字符串的长度
        string s1 = "helloworld";
        system.out.println(s1.length());
        //2.返回某索引处的字符
        system.out.println(s1.charat(1));
        //3.判断字符串是否是空字符串
        system.out.println(s1.isempty());
        //4.将string中的所有字符串转换成小写
        string s2 = "shopping";
        string s3 = s2.tolowercase();
        system.out.println(s3);
        //5.将string中的所有字符串转换成大写
        string s4 = s2.touppercase();
        system.out.println(s4);
        //6.返回字符串的副本,忽略前导空白和尾部空白
        string s5 = "  an  dro  id   ";
        string s6 = s5.trim();
        system.out.println("**********"+s5+"**********");
        system.out.println("**********"+s6+"**********");
        //7.比较字符串的内容是否相同
        system.out.println(s1.equals(s5));
        //8.与equals方法类似,这个忽略大小写
        string s7="abcdef";
        string s8="abcdef";
        system.out.println(s7.equals(s8));//false
        system.out.println(s7.equalsignorecase(s8));//true
        //9.将指定字符串连接到此字符串的结尾,等价于"+"
        string s9="abc";
        string s10 = s9.concat("def");
        system.out.println(s10);
        //10.比较两个字符串的大小
        string s11="abe";
        system.out.println(s9.compareto(s11)); //-2  说明s9小于s11
        //11.返回一个新的字符串,它是此字符串的从bedinindex开始截取到最后的一个子字符串
        string s12 = "我一定要学会android";
        system.out.println(s12.substring(6));
        //12.返回一个新字符串,它是此字符串从beginindex开始截取到endindex(不包括)的一个子字符串
        string s13 = s12.substring(2, 6);
        system.out.println(s13);
    }

输出结果如下:

当然string中,不止这些方法,只不过这些是比较常用的方法。
下面再说一些其他的方法:
还是以代码为例:

@test
    public void test2(){
        //1.测试此字符串是否以指定的后缀结束
        string s1 = "helloworld";
        system.out.println(s1.endswith("ld"));//true
        //2.测试此字符串是否以指定的前缀结束
        system.out.println(s1.startswith("hel"));//true
        //3.测试此字符串从指定索引开始的字符串是否以指定前缀开始
        system.out.println(s1.startswith("ow", 4));//true
        //4.当且仅当此字符串包含指定的char值序列时,返回true;
        system.out.println(s1.contains("lo"));//true
        system.out.println(s1.contains("lowld"));//false
        //5.返回指定子字符串在此字符串中第一次出现处的索引
        system.out.println(s1.indexof("el"));//1
        //6.返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
        system.out.println(s1.indexof("ow",3));//4
        //7.返回指定子字符串在此字符串中最右边出现处的索引
        system.out.println(s1.lastindexof("o"));//6
        //8.返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
        system.out.println(s1.lastindexof("o", 5));//4
    }

下面是string中关于正则的方法:

@test
    public void test3(){
        //1.返回一个新的字符串,它是通过用newchar替换此字符串中出现的所有oldchar得到的
        string s1="你好,我是程序员小白,小白!";
        system.out.println(s1.replace('小','大'));
        //2.使用指定的字面值替换序列,替换此字符串所有匹配字面值目标序列的子字符串
        system.out.println(s1.replace("小白","大牛"));
        //3.使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串
        string s2="12hello2342world234android";
        string s3 = s2.replaceall("\\d+", ",").replaceall("^,|,$", "");
        system.out.println(s3);
        //4.告知此字符串是否匹配给定的正则表达式
        string s4="123456";
        //判断s4字符串中是否全部由数字组成,即1-n个数字组成
        boolean matches = s4.matches("\\d+");
        system.out.println(matches);
        string tel="0373-12345678";
        //判断这是否是河南的一个固定电话
        boolean matches1 = tel.matches("0373-\\d{7,8}");
        system.out.println(matches1);
        //5.根据给定正则表达式的匹配拆分此字符串
        string s5="hello|world|android";
        string[] split = s5.split("\\|");
        for (int i = 0; i < split.length; i++) {
            system.out.println(split[i]);
        }
        system.out.println("****************************");
        
        //6.根据匹配给定的正则表达式来拆分此字符串,最多不能超过limit个,如果超过了,剩下的都全部放到最后一个元素中
        string s6="hello.world.android";
        string[] split1 = s6.split("\\.");
        for (int i = 0; i < split1.length; i++) {
            system.out.println(split1[i]);
        }
    }

输出结果如下:

到此这篇关于浅谈java中string的常用方法的文章就介绍到这了,更多相关string的常用方法内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!