《Java基礎知識》Java字符串詳解


 

本文內容:

String類的概述

String類的使用步驟

String類的常用方法

本文目的:

能夠使用String類的構造方法創建字符串對象

能夠明確String類的構造方法創建對象,和直接賦值創建字符串對象的區別

能夠使用文檔查詢String類的判斷方法

能夠使用文檔查詢String類的獲取方法

能夠使用文檔查詢String類的轉化方法

一、String類

概述java.lang.String 類代表字符串。Java程序中所有的字符串文字(比如"abc")都可以被看成是實現此類的實例。類String中包括用在檢查每一個字符串的方法,比如用於比較字符串,搜索字符串,提取字符串以及創建具有翻譯為大寫或者是小寫的所有字符的字符串副本。

特點(1)字符串是不變的,字符串的值在創建以后是不可以被更改的

案例:

public class var {
    public static void main(String[] args) {
        String str = "abc";
        str += "d";
        System.out.println(str);
    }
}

運行結果:

特點(2)因為String對象是不可以變換的,所以它們才可以被共享。

public class var {
    public static void main(String[] args) {
        //內存中只有一個“abc”被創建。
        String s1 = "abc";
        String s2 = "abc";
    }
}

特點(3)"abc"等於char[ ] data={′a′,′b′,′c′}

public class var {
    public static void main(String[] args) {
        char[] data={'a','b','c'};
        String str = new String(data);
        System.out.println(str);
    }
}

運行結果:

3. 使用步驟

 查看類

java.lang.String:此類不需要再導入

public String( ):初始化新創建的String對象,可以讓它表示空字符序列

public String(char[ ] value):通過當前參數當中的字符數組來構造新的String

public String(byte[ ] bytes):通過使用平台的默認字符集解碼當前參數中的字節數組來構造新的String

構造舉例,代碼如下所示:

public class var {
    public static void main(String[] args) {
        //無慘構造
        String str0 = new String();
        //通過字符數組構造
        char[] data={'a','b','c'};
        String str1 = new String(data);

        //通過字節數組構造
        byte[] bytes={97,98,99};
        String str2 = new String(bytes);
    }
}

4.常用的方法

(1)判斷功能的方法

案例:

public class var {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        String s3 = "HELLO";
        System.out.println("------------通過equals比較-----------------");
        if(s1.equals(s2)){
            System.out.println("s1.equals(s2): true");
        }
        if(!s1.equals(s3)){
            System.out.println("s1.equals(s3): false");
        }
        System.out.println("------------通過equalsIgnoreCase比較-----------------");
        if(s1.equalsIgnoreCase(s2)){
            System.out.println("s1.equals(s2): true");
        }
        if(s1.equalsIgnoreCase(s3)){
            System.out.println("s1.equals(s3): true");
        }
    }
}

運行結果:

(2)獲取功能的方法:

public class var {
    public static void main(String[] args) {
        String s1 = "hello";
        //獲取字符串長度
        System.out.println("字符串長度:"+s1.length());
        //拼接字符串
        String s2 = s1.concat("world");
        System.out.println("字符串s1拼上world后:"+s2);
        //獲取指定位置元素
        char c1= s1.charAt(0);
        System.out.println("字符串s1第一位:"+c1);
        //獲取字符串中出現指定元素的位置
        int i = s1.indexOf("e");
        System.out.println("字符串s1中‘e’所在位置:"+i);
        //截取字符串
        String s3 = s1.substring(0,2);
        System.out.println("截取字符串s1中1到2的元素:"+s3);
    }
}

運行結果:

(3)轉換功能的方法:

 

public class var {
    public static void main(String[] args) {
        String s1 = "hello";
        //字符串轉成字符數組
        char[] arr = s1.toCharArray();
        for (int i = 0; i < arr.length ; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        //轉換成字節
        byte[] b1 = s1.getBytes();
        for (int i = 0; i < b1.length ; i++) {
            System.out.print(b1[i] + " ");
        }
        System.out.println();
        //字符替換
        String s2 = s1.replace("ll","LL");
        System.out.println("替換后的字符串為"+s2);
    }
}

運行結果:

(4)分割功能的方法:

public class var {
    public static void main(String[] args) {
        String s1 = "hello,world";
        String[] arr = s1.split(",");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

運行結果:

 

參考:https://baijiahao.baidu.com/s?id=1622780499548219487&wfr=spider&for=pc


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM