String类的相关方法介绍

来源:互联网 发布:如何评价中国股市知乎 编辑:程序博客网 时间:2024/06/05 09:33
public class Test{    public static void main(String []args){        //1.返回指定索引处的char值        String str="helloworld";        char result=str.charAt(5);        char result1=str.charAt(6);        System.out.println(result);        System.out.println(result1);        //2.按字典顺序比较两个字符串的大小,返回一个int类型的差值        String str1="a";        String str2="c";        String str3="A";        int result2=str1.compareTo(str2);        int result3=str1.compareTo(str3);        System.out.println(result2);        System.out.println(result3);        //3.不考虑大小写,按字典顺序比较两个字符串        String str4="d";        String str5="D";        String str6="A";        String str7="E";        int result4=str4.compareToIgnoreCase(str5);        int result5=str4.compareToIgnoreCase(str6);        int result6=str4.compareToIgnoreCase(str7);        System.out.println("值相等为:"+result4);        System.out.println("值为正为:"+result5);        System.out.println("值为负为:"+result6);        //4.将指定字符串连接到字符串的结尾        String str8="hello";        String str9="world";        String result17=str8.concat(str9);        System.out.println(result17);        //5.在给定的字符串序列中包含指定的字符串序列时,有则返回true        String str10="helloworld";        boolean iden=str10.contains("hello");        boolean iden1=str10.contains("z");        System.out.println("包含指定的字符串序列"+iden);        System.out.println("不包含指定的字符串序列"+iden1);        //6.在给定的字符串序列中与指定的字符串序列相等时,有则返回true        String str11="helloworld";        String str12="hello";        String str13="helloworld";        boolean iden2=str11.contentEquals(str13);        boolean iden3=str11.contentEquals(str12);        System.out.println("与指定字符串相等"+iden2);        System.out.println("与指定字符串不相等"+iden3);        //7.把字符数组转换成字符串        char []arr={'h','e','l','l','o'};        String result18=String.copyValueOf(arr);        System.out.println("将指定数组转换为字符串为"+result18);        //8.返回指定数组中表示该字符序列的字符串        char []arr1={'h','e','l','l','o'};        String result19=String.copyValueOf(arr,2,3);        System.out.println("返回一个数组中的llo字符"+result19);        //9.测试此字符是否以指定的后缀结束        String str14="helloworld";        boolean iden4=str14.endsWith("d");        boolean iden5=str14.endsWith("world");        System.out.println("字符以d后缀结束"+iden4);        System.out.println("字符以world后缀结束"+iden5);        //10.比较两个字符串的内容是否相等        String str15="hello";        String str16="hello";        String str17="world";        boolean iden6=str15.equals(str16);        boolean iden7=str15.equals(str17);        System.out.println("比较两个字符串的内容"+iden6);        System.out.println("比较两个字符串的内容"+iden7);        //11.不区分大小写,比较两个字符串的内容是否相等        String str18="hello";        String str19="HELlO";        String str20="world";        boolean iden8=str18.equalsIgnoreCase(str19);        boolean iden9=str18.equalsIgnoreCase(str20);        System.out.println(iden8);        System.out.println(iden9);        //12.返回一个指定字符在此字符串中第一次出现的位置        String str21="helloworld";        int result20=str21.indexOf("o");        int result22=str21.indexOf("w");        int result23=str21.indexOf("world");        System.out.println("获取字符串o第一次出现的位置"+result20);        System.out.println("获取字符串w第一次出现的位置"+result22);        System.out.println("获取字符串world第一次出现的位置"+result23);        //13.从指定的索引处开始搜索,返回在此字符串中第一次出现指定子字符处的索引        String str22="helloworld";        String str23="o";        String str24="o";        int result24=str22.indexOf(str23,5);//从下标为5的地方开始遍历指定字符o的位置        int result25=str22.indexOf(str24,0);//从下标为0的地方开始遍历指定字符o的位置        System.out.println(result24);        System.out.println(result25);        //14返回字符串对象的规范化表示形式        String str25="helloworld";        String result26=str25.intern();        System.out.println(result26);        //15.返回最后一次出现的指定字符在此字符串中的索引        String str26="helloworld";        int result27=str26.lastIndexOf('l');        int ch2='o';        int result28=str26.lastIndexOf(ch2);        System.out.println("最后一次出现字符l的下标为:"+result27);        System.out.println("最后一次出现字符o的下标为:"+result28);        //16.从指定索引处开始进行向后搜索,返回最后一次出现的指定字符在此字符串中的索引        String str27="helloworld";        //int ch3='o';        int result29=str27.lastIndexOf("l",9);        int result30=str27.lastIndexOf("o",5);        System.out.println(result29);        System.out.println(result30);        //17.返回在此字符串中最右边出现的指定字符串的索引        String str28="helloworld";        String str29="o";        int result31=str28.lastIndexOf("l");        int result32=str28.lastIndexOf(str29);        System.out.println(result31);         System.out.println(result32);        //18.从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。        String str30="helloworld";        String str31="llo";        int result33=str30.lastIndexOf(str31,9);        System.out.println("从下标为5的位置向后搜索llo为:"+result33);        //19.返回此字符串的长度        String str32="hello world!";        int result34=str32.length();        System.out.println(result34);        //20.把字符串内容进行替换,然后然后输出新字符串        String str33="helloworld";        String str34="郑忠燕";        String result35=str33.replace(str33,str34);        String result035=str33.replace('o','y');        System.out.println("将helloworld整体替换为:"+result35);        System.out.println("将字符串中的o替换成y为:"+result035);        //21.把字符串中指定的内容进行替换,然后输出新字符串        String str35="helloworld";        String str36="龙卷风";        String result36=str35.replace("world",str36);        System.out.println(result36);        //22.撕裂        String str37="zheng,zhong,yan";        String []arr2=str37.split(",");        int len=arr2.length;        System.out.println("撕裂后的数组长度为:"+len);        for(int i=0;i<len;i++){            System.out.println(arr2[i]);        }        //23.正则表达式        boolean iden10="ababudu".matches("^a+$");        System.out.println(iden10);        //24.测试此字符串是否以指定的前缀开始。        String str38="zhenghelloworld";        boolean iden11=str38.startsWith("zheng");        boolean iden12=str38.startsWith("hello");        System.out.println(iden11);        System.out.println(iden12);        //25.测试此字符串是否以指定前缀开始,该前缀以指定索引开始。        String str39="helloworldzhengzhongyan";        boolean iden13=str13.startsWith("zheng",10);        boolean iden14=str13.startsWith("hello",0);        System.out.println("该前缀不是以指定索引的开始"+iden13);        System.out.println("该前缀是以指定索引的开始"+iden14);        //26.返回一个新的字符序列,它是此序列的一个子序列。        String str40="hello world";        String result37=str40.substring(6);        System.out.println(result37);        //26.返回一个子字符串,从下标开始的位置开始,从下标结束的下一个位置结束        String str41="hello world";        String result38=str41.substring(0,5);        System.out.println(result38);        //27.将此字符串转换为一个新的字符数组。        String str42="hello";        char[]arr3=str42.toCharArray();        for(int i=0;i<arr3.length;i++){            System.out.print(arr[i]+" ");        }    }}

这里写图片描述

0 0
原创粉丝点击