java基礎知識1--String經常使用方法總結

主要涉及String經常使用的方法。java

package collection;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created by wyy on 2017/2/16.
 */
public class StringTest {
    public static void main(String[] args) {

//String的用法

        //輸出第3個字符
        String str="WATERMELON";
        String st="lalaland";
        System.out.println(str.charAt(2));

        //將字符數組轉化爲字符串
        char[] ch={'a','b','c','d'};
        String c=new String(ch);//abcd
        String c1=new String(ch,2,1);// cd
        System.out.println(c1);


        //String的比較用compareTo方法,輸出長度差,它從第一位開始比較,若是遇到不一樣的字符,則立刻返回這兩個字符的ascii值差值..返回值是int類型
        String  strr="WA";
        String  s="wa";
        int a=str.compareTo(strr);
        int b=str.compareToIgnoreCase(strr);//忽略大小寫
        System.out.println(a);

        //是否以某個字符串開頭,結尾
        System.out.println(str.startsWith("W"));
        System.out.println(str.endsWith("W"));

        //輸出匹配的字母在字符串的位置
        int m=str.indexOf("T");
        System.out.println(m);
        System.out.println(str.indexOf(2,3)); // ????、

        //取子字符串
        System.out.println(str.substring(3));  //子字符串,從第4個字符日後,ERMELON
        System.out.println(str.substring(3,5));//子字符串,從第4個字符日後,到第6個,ER

        //改變大小寫
        System.out.println(str.toLowerCase());//改爲小寫   watermelon
        System.out.println(st.toUpperCase());//改爲大寫  LALALAND

        //equals()方法比較字符串對象中的字符,==運算符比較兩個對象是否引用同一實例
        String s1="Hello";
        String s2=new String(s1);
        s1.equals(s2); //true   ,若是用  == 判斷則會報錯
        System.out.println(strr.equalsIgnoreCase(s));//比較相等,忽略大小寫   true

     /*   鏈接兩個字符串,此處比較:
     StringBuffer.append(),Stringbuffer是可變的,建立一個StringBuffer對象後,鏈接後,對象的值也會變
     String的concat(),只能用於字符串,String是final修飾的類,是不可變的,每次必須新建一個對象接收concat,纔會有變化
     而+能夠用於任何類型的鏈接
*/
        StringBuffer sb1=new StringBuffer("love");
        sb1.append("rose");
        String h="I LIKE KE";
        h.concat("rose");
        System.out.println(sb1); //sb1的值直接變了,loverose
        System.out.println(h);//h.concat("rose")賦給新的String變量才能變,I LIKE
        System.out.println(h+"you");//I LIKEyou


        //替換字符串中的字符或字符串 replace()
        String h1=h.replace('I','A');//將全部的I替換成A
        String h2=h.replaceFirst("KE","OO");//將第一個的KE替換成OO
        String h3=h.replaceAll("KE","OO");//將全部的KE替換成OO
        System.out.println(h1);
        System.out.println(h2);
        System.out.println(h3);

        //trim(),去掉字符串先後的空格,若是替換所有,能夠用replaceALL(" ","")
        String k="  lala da do ";
        String k1=k.trim();
        String k2=k.replace(" ","");
        System.out.println(k1);
        System.out.println(k2);

        //contains()是否包含某個字符串或字符
        System.out.println(k.contains("lala"));// true

        /*split(),返回的是個字符串數組,
        注意對於字符"|","*","+"都得加上轉義字符,前面加上"\\",
        而若是是"\",那麼就得寫成"\\\\"。
        若是一個字符串中有多個分隔符,能夠用"|"做爲連字符。*/
        String l="wa.ni.huhu";
        String l2="wo%ai#chi";
        String[] l1=l.split("\\.");
        for (String j:l1
             ) {
            System.out.println(j);
        }

        String[] l3=l2.split("%|#"); 
        for (String n:l3
                ) {
            System.out.println(n); //wo ai chi
        }

     /*將字符串轉化爲基本數據類型,用Int.parseInt(string)
       包括Byte、Short、Integer、Float、Double類   */

       String n3="12.34";
       float n1=Float.parseFloat(n3); //12.34
       double n2=Double.parseDouble(n3);//12.34
       System.out.println(n1);
       System.out.println(n2);


     /* 將基本的數據類型轉化爲String類型,用 String.valueOf()*/
        int p1=7;
        String p2=String .valueOf(p1)+"ww";
        System.out.println(p2);  //7ww
        
/*Stringbuilder,字符串構建器,用於構建鏈接短的字符串,避免每次鏈接字符串產生一個新的string對象*/
        StringBuilder sb=new StringBuilder();
        sb.append("wawa");//追加字符串或字符
        sb.append("hhhh");
        sb.append(str);
        sb.insert(2,"i");//在第三個位置插入i
        sb.delete(4,6);//waiwhhhWATERMELON  刪除了第4,5個字符
        System.out.println(sb); //waiwahhhhWATERMELON
        sb.toString();//構建一個與構建器內容相同的字符串


//字符串數組
        /*1.定義數組必需要定義大小,而arraylist是動態的,能夠不用定義大小*/

        String[] str1=new String[]{"b","c"};
        String[] str2=new String[2];
        str2[1]="dd";  //不能總體定義

//        輸出整個字符串
        System.out.println(str1.toString()); //[Ljava.lang.String;@1540e19d
        System.out.println(Arrays.toString(str1)); //Arrays.toString()  把結果轉化爲字符串

//        foreach循環
        for (String n:str1
             ) {
            System.out.println(s);
        }
//        for循環

        for (int i = 0; i < str1.length; i++) {
            System.out.println(str1[i]);

        }
    }


}
相關文章
相關標籤/搜索