Java String類

概述

 字符串廣泛應用 在 Java 編程中,在 Java 中字符串屬於對象,Java 提供了 String 類來創建和操作字符串。
jdk中提供非常多的字符和字符串操作方法及構造方法,這裡只介紹一些常用的方法和構造方法。完整的String類下的方法可以參考官方的API文檔。
本地API文檔下載: //kohler.lanzouv.com/ikIfV078pbhe
在線API文檔://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html

API文檔截圖:

對象創建

直接使用字面值

可以直接定義String類型的變量直接給其賦值一個字符串字面值
例:

String name = "愷龍";

使用構造方法

可以使用String中定義的構造方法來創建對象。String類下有非常多的構造方法,這裡只介紹幾個常用的。

String()

public String();
初始化新創建的字符串對象,使其表示空字符序列。

示例代碼:

 public static void main(String[] args) {
		//使用無參構造創建。字符串的內容為空 相當於 ""
        String s1 = new String();
    }

String(byte[] bytes)

String(byte[] bytes);
將數組轉換為字符串。
示例代碼:

 public static void main(String[] args) {
		
        byte[] bytes = {68,74,84,85};
        String s = new String(bytes);
        System.out.println(s);//輸出結果為DJTU,這裡是將數字通過ASC碼錶轉換為了字母
    }

結果:

String(byte[] bytes, int offset, int length)

通過使用平台的默認字符集解碼指定的 byte 子數組,構造一個新的 String。
參數:
bytes:要解碼為字符的 byte
offset: 要解碼的第一個 byte 的索引
length: 要解碼的 byte 數 的長度

示例代碼:

 public static void main(String[] args) {
        byte[] bytes = {68,74,84,85};
        String s = new String(bytes,0,2);
        System.out.println(s);//輸出結果為DJ,從第0個開始長度為2個
        String s2 = new String(bytes,0,1);
        System.out.println(s2);//輸出結果為D,從第0個開始長度為1個
    }

結果:
String(char[] value)

轉換字符數組為字符串類
示例代碼:

 public static void main(String[] args) {
        char[] chars = {'D','J','T','U'};
        String s = new String(chars);
        System.out.println(s);//輸出結果為DJTU
    }

結果:
String(char[] value, int offset, int count)

參數:
value – 作為字符源的數組。
offset – 初始偏移量。
count – 長度。
就是在數組value上選取一部分成為String對象。
示例代碼:

 public static void main(String[] args) {
        char[] chars = {'D','J','T','U'};
        String s = new String(chars,0,1);
        System.out.println(s);//輸出結果為D
        String ss = new String(chars,0,2);
        System.out.println(s2);//輸出結果為DJ
    }

結果:

常用方法

方法 解釋
String[] split(String regex) 把一個字符串按照指定的分隔符切割成多個字符串,把多個字符串放在一個字符串數組中返回
char[] toCharArray(); 把一個字符串的內容轉換成一個字符數組
byte[] getBytes(); 把一個字符串的內容轉換成一個byte數組
String substring(int index); 把某個字符串從index索引開始截取到最後
String substring(int begin,int end) 把某個字符串索引begin到索引end截取出來
boolean equals(Object anObject) 判斷兩個字符串的內容是否相同

split方法演示

    public static void main(String[] args) {
        String s = "DJTU,China,LiaoNing,DaLian";
        String[] strs = s.split(",");//以,分割
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
    }

結果:

toCharArray方法演示

 public static void main(String[] args) {
        String s = "DJTU";
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.println(chars[i]);
        }
    }

結果:

getBytes方法演示

    public static void main(String[] args) {
        String s = "DJTU";
        byte[] bytes = s.getBytes();//按照ASC碼錶轉換為數字
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
    }

結果:

substring方法演示

    public static void main(String[] args) {
        String s = "DJTU";
        String substring = s.substring(1);//從第[1]個開始截取
        System.out.println(substring);
    }

結果:

    public static void main(String[] args) {
        String s = "DJTU";
        String substring = s.substring(1,2);//從第[1]個開始到第[2]個結束(不包含第[2]個)
        System.out.println(substring);
    }

equals方法演示

    public static void main(String[] args) {
        String s = "DJTU";
        String s2 = "DJTU";
        String s3 = "DJTUD";
        boolean flag = s.equals(s2);
        boolean flag1 = s.equals(s3);
        System.out.println(flag);//輸出true
        System.out.println(flag1);//輸出false
    }

結果:

特點

  1. 一個字符串一旦創建其內容是永遠不會變的
  2. 字符串效果上相當於是char[]字符數組,但是底層其實是byte[]位元組數組

如圖片失效等問題請參閱公眾號文章://mp.weixin.qq.com/s/_vctLlUqXqy7_vWBwYaFgg

歡迎關注我的公眾號:”愚生淺末”,一起交流學習。

Tags: