JAVA 學習模塊二十: String 類

這裏寫圖片描述

1.

public class Stringdemo {

        public  static void main(String[] args){

            /*
             String 類的特點 
             String 類:
                1.字符串是一個特殊的對象
                2.字符串一旦初始化就不可改變
                3.string str = “abc” 與 String str1 = new String("abc") 的區別
             */


            stringdemo2();


        }

        /**
         *演示字符串定義的第一種方式,並明確字符串常量值, 
         *池中沒有就建立,池中有就直接用
         */
        protected static void stringdemo1() {
            String s = "abc";
            String s1 = "abc";
            System.out.println(s == s1);// ture ? 地址一樣?   java會把已有的 abc 的地址賦給 s1,abc 存儲在字符串常量池中
        }

        protected static void stringdemo2() {
            String s = "abc";  //在常量池中創建一個對象
            String s1 = new String("abc");//在堆裏創建一個對象,並在構造時接受了一個字符串,產生兩個對象
            System.out.println(s == s1);// false 
            System.out.println(s.equals(s1));// ture  string類中的equals 複寫了 object 的equal,建立自己的equals不比較地址,比較內容
        }


}
package day15.p1.demo;

public class StringConductDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StringConductDemo();
        StringConductDemo1();
        StringConductDemo3();


    }

    private static void StringConductDemo3() {
        // TODO Auto-generated method stub

        String s = "zhang,li,wang";
        String[] arr = s.split(",");
        for (int i = 0; i < arr.length;i++)  // .是特俗符號,用//.轉義
        {
            System.out.println(arr[i]);
        }


    }

    public static void StringConductDemo1() {
        // TODO Auto-generated method stub
        /*
         * 按照面向對象的思想對字符串的功能進行分類
         * 1.獲取:
         * 1.1獲取字符串中字符的個數(長度)  int  length();
         * 1.2根據位置獲取字符  char charAt(int index)
         * 1.3 根據字符獲取位置 int  indexof(int ch) 第一次出現的位置
         *      int indexof(int ch, int fromIndex) 從指定位置進行查找 字符出現的第一次
         *      int lastIndexof()
         * 1.4獲取字符串中的一部分,也叫字串
         *      String substring(int begin int end )開始包含索引,結束不包含索引
         * 
         * 22.轉換
         * 2.1 將字符串切成幾段,即變成字符串數組,用切割方法 String【】  Split(String regex) 例:
         * 2.2 將字符串變成字符數組, String toCharArray()
         * 2.3 將字符串轉換成字節數組 byte[] getBytes() //一箇中文來兩個字節
         * 2.4將字符串中的字母轉換大小寫。  String  toUpperCase() //toLowerCase
         * 2.5。將字符串中的內容進行替換()   String (char oldchar, char newchar)
         * 2.6 trim() 可以去除字符串兩端字符,獲取信息時用
         * 2.7 將字符串進行來連接 concat
         * 
         * 
         * 3.判斷
         * 3.1 判斷字符串內容是否相同 boolean equals(Object obj )  大小寫不同   equalsIgnoreCase()
         * 3.2 包含 contains
         * 3.2 字符差U呢是否以指定字符差U呢開頭結尾   boolean startswith(string)/ endswith()
         * 
         * 4.比較
         * 
         * 4.1compareto() 按字典順序比較兩個字符串大小,不分大小
         * */
    }

    public static void StringConductDemo() {
        // TODO Auto-generated method stub
        String s = new String();
        byte[] arr = {65,66,67,68};
        String s1 = new String(arr);//將字節數組變成字符串
        System.out.println(s1);
        char[] arr1 = {'a','b','c'};
        String s2 = new String(arr1);//輸出 ABCD
        String s3 = new String(arr1,1,2);//輸出 ABCD
        System.out.println(s2);// 輸出 abc
        System.out.println(s3);// 輸出 bc
        System.out.println(s3.length());// 輸出  2




    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章