String类的常用方法

    

        String tom=new String("we are students");
        String body=new String("you are students");
        String jerry=new String("we are students");


/**int length()*/
        System.out.println(tom.length());/*获取tom字符串的长度*/



/**boolean equals(String s)*/
        System.out.println(tom.equals(body));   //比较当前字符串对象的实体是否与参数body指定的字符串的实体相同。正确返回true 否则返回false



/**boolean startsWith(String s)*/
        System.out.println(tom.startsWith("we"));   //startsWith比较当前字符串对象的前缀是否是参数"we"指定的字符串.  正确返回true 否则返回false



/**boolean regionMatches(int firstStart,String other,int otherStart,int length)*/
        System.out.println(tom.regionMatches(5,"e s",0,3));     /*从当前字符串tom位置“5”处,取长度为“3”的一个子串,并将这个子串和参数“e s”从“0”位置开始取长度为“3”进行比较,正确返回true 否则返回false*/



/**int compareTo(String s)*/
        if(tom.compareTo(jerry)>0){     /*tom字符串按字典序与参数(jerry)字符串比较大小,相同返回0,大于返回正值,小于返回负值。*/
            System.out.println("tom大于jerry");
        }else if(tom.compareTo(jerry)<0){
            System.out.println("tom小于jerry");
        }else if(tom.compareTo(jerry)==0){
            System.out.println("tom等于jerry");
        }



/**boolean contains(String s)*/
        if(tom.contains("we")){     /*tom字符串中是否含有“we”字符串,含有返回true,否则返回false*/    
            System.out.println("tom字符串中含有“we”");
        }



/**int indexOf(String s)*/
        System.out.println(tom.indexOf("e"));/*从tom字符串的头开始检索字符“e”,并返回首次发现“e”的位置*/


        System.out.println(tom.indexOf("e",3));/*从tom字符串的“3”位置开始检索字符“e”,并返回首次发现“e”的位置*/

        


/**int lastIndexOf(String s)*/
        System.out.println(tom.lastIndexOf("e"));/*从tom字符串的尾开始检索字符“e”,并返回首次发现“e”的位置*/



/**String substring(int startpoint)*/
        System.out.println(tom.substring(3));   /*获得从tom字符串的"3"位置开始截取到最后的字符串*/


        System.out.println(tom.substring(3,5));     /*获得从tom字符串的"3"位置开始截取到"5"位置(不包括5)的字符串*/  
       


/**String trim()*/
        System.out.println(tom.trim());     /*获取tom字符串去掉前后空格后所剩字符串*/

        


/**void getChars(int start,int end,char c[],int offset)*/
        char a[]=new char[50];
        tom.getChars(0,5,a,3);  /*将tom字符串从“0”位置开始到“5-1”位置截取子串赋给a数组,a数组从“3”位置开始存放这些字符*/
        System.out.println(a);



/**String concat();*/     

        System.out.println(tom.concat(tom));

        /**在tom后面追加tom字符串*/



/**String replace();*/  

        System.out.println(tom.replace("e","a"));

        /**将tom中的e替换成a*/