Java的string、StringBuffer、StringBuilder

目錄

1.三者的區別

2.String

2.1字符串本質

2.2構造方法

2.2字符串的比較

2.3查找、搜索字符串

2.4大小寫轉換

2.5字符串長度、去空格、分割

2.6字符串截取、替換、連接

2.7 getBytes()、toCharArray

3.StringBuffer

4.StringBuilder

1.三者的區別

String、StringBuffer和StringBuilder的區別 -   https://blog.csdn.net/csxypr/article/details/92378336

StringBuilder類也代表可變字符串對象。實際上,StringBuilder和StringBuffer基本相似,兩個類的構造器和方法也基本相同。不同的是:StringBuffer是線程安全的,而StringBuilder則沒有實現線程安全功能,所以性能略高。

2.String

JAVA String字符串的常用方法 - 知乎  https://zhuanlan.zhihu.com/p/64093275

java中常用的String方法 - 阿溪 - 博客園  https://www.cnblogs.com/liujiquan/p/7808501.html

2.1字符串本質

String 類代表字符串。Java 程序中的所有字符串字面值(如“abc”)都作爲此類的對象。字符串本質上是一個字符數組,它們的值在創建後不能被更改,所以字符串是常量;可以把字符串看成是字符數組的包裝類,內部聲明一個 private final char value [ ];因爲String 對象是不可變的,所以可以共享。

2.2構造方法

    public static void main(String[] args) {
        // 在堆區初始化一個空字符串
        String str1 = new String();

        // 通過一個字節數組構建一個字符串
        byte[] bytes = {97,98,99};
        // 通過使用平臺的默認字符集解碼指定的 byte 數組
        // System.out.println(Charset.defaultCharset());
        String str2 = new String(bytes);
        System.out.println(str2);//abc

        byte[] byte2 = {-42,-48};
        String str3 = null;
        try {
            // 使用指定的字符集對字節序列進行解碼
            str3 = new String(byte2,"GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(str3);//中

        // 需求:對一個utf-8的字節序列進行解碼
        byte[] byte3 = {-28,-72,-83,-28,-72,-83};
        try {
            // sssString str4 = new String(byte3, "UTF-8");
            String str4 = new String(byte3,0,3, "UTF-8");
            System.out.println("str4:"+str4);//str4:中
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // 通過字符數組構建字符串
        char[] c1 = {'a','b','c','中','國'};
        // String str5 = new String(c1);
        String str5 = new String(c1,0,3);
        System.out.println(str5);//abc

        String str6 = new String("abc");
        String str7 = "abc";
        System.out.println(str6 == str7);//false

    }

2.2字符串的比較

        String a = "Hello Word";
        String b = "hello word";
        System.out.println(a == b);//false
        System.out.println(a.equals(b));//false
        System.out.println(a.equalsIgnoreCase(b));//true
        String c = a;
        System.out.println(a == c);//true
        System.out.println(a.equals(c));//true
        System.out.println(a.equalsIgnoreCase(c));//true

//compareTo()和compareToIgnoreCase()按字典順序比較兩個字符串的大小,前者區分大小寫,後者不區分
        String a = "Hello Word";
        String b = "hello word";
        System.out.println(a.compareTo(b));//-32
        System.out.println(a.compareToIgnoreCase(b));//0

        String str="我是123,123。123";
        //判斷字符串的開始和結尾
        boolean startWith=str.startsWith("我");
        boolean endWith=str.endsWith("3");

2.3查找、搜索字符串

        String str="我是123,123。123";
        //獲取字符串指定位置的字符
        char indexChar = str.charAt(0);
        System.out.println(indexChar);//我
        //查找某個字符在字符串中首次出現的位置
        int firstIndex = str.indexOf("1");
        System.out.println(firstIndex);//2
        //查找某個字符在字符串中最後出現的位置
        int lastIndex = str.lastIndexOf("1");
        System.out.println(lastIndex);//10
        boolean contains = str.contains("10");
        System.out.println(contains);//false

2.4大小寫轉換

        String str="我是Aa";
        //將字母全部轉換成大寫
        String lowerCase = str.toLowerCase();
        System.out.println(lowerCase);//我是aa
        //將字母全部轉換成小寫
        String upperCase = str.toUpperCase();
        System.out.println(upperCase);//我是AA

2.5字符串長度、去空格、分割

        String str=" 我是123,123。123 ";
        //獲取字符串長度
        int strLenth = str.length();//15
        System.out.println(strLenth);
        //去除字符串中的首尾空格
        String newStr = str.trim();//我是123,123。123
        System.out.println(newStr);
        //將字符串分割,多個分隔符可用|隔開,例如下面這個是按照“。”和“,”分割的
        String[] splite = str.trim().split("。|,");//
        for (int i = 0; i < splite.length; i++)
            System.out.println(splite[i]);
        //我是123
        //123
        //123

2.6字符串截取、替換、連接

        String str="我是123,123。123";
        //截取字符串
        String subStr = str.substring(0,2);//我是
        //字符串替換
        String reStr = str.replace("1", "4");//我是423,423。423
        //替換第一次出現的字符
        String firReStr = str.replaceFirst("1", "4");//我是423,123。123
        //連接字符串
        String a = "Hello Word";
        String b = "你好";
        System.out.println(b.concat(a));//你好Hello Word
        System.out.println(b+a);//你好Hello Word

2.7 getBytes()、toCharArray

        String a = "Hello Word";
        //getBytes()將字符串變成一個byte數組
        byte[] b = a.getBytes();
        System.out.println(b);//[B@1cd072a9
        System.out.println(new String(b));//Hello Word
        //toCharArray()將字符串變成一個字符數組
        char[] c = a.toCharArray();
        System.out.println(c);//Hello Word

3.StringBuffer

append(String s):將指定的字符串追加到此字符序列。

Reverse():將此字符序列用其反轉形式取代。

delete(int start, int end):移除此序列的子字符串中的字符。

insert(int offset, int i):將 int 參數的字符串表示形式插入指定位置。

replace(int start, int end, String str):使用給定 String 中的字符替換此序列的子字符串中的字符。

 StringBuffer strBuffer1 = new StringBuffer("Hello");
        StringBuffer strBuffer2 = new StringBuffer("*");
        strBuffer1.append(strBuffer2);
        strBuffer1.append("wo"+"rld");
        strBuffer1.append("!");
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Hello*world!
        // 獲取StringBuffer對象的容量,和字符串的長度
        System.out.println("strBuffer1.capacity() = "+strBuffer1.capacity());//strBuffer1.capacity() = 21
        System.out.println("strBuffer1.length() = "+strBuffer1.length());//strBuffer1.length() = 12
        // 刪除指定位置字符串
        strBuffer1.replace(0, strBuffer1.indexOf("*"), "Ni Hao,");
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao,*world!
        strBuffer1.deleteCharAt(strBuffer1.indexOf("*"));
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao,world!
        // 將指定的字符串插入字符串序列
        strBuffer1.insert(strBuffer1.indexOf(",")+1, " ");
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao, world!
        // 字符串反轉
        strBuffer1.reverse();
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = !dlrow ,oaH iN
        strBuffer1.reverse();
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao, world!
        // 將指定位置的字符串替換並指定字符串的長度
        strBuffer1.setCharAt(0, 'n');
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = ni Hao, world!
        // 比較StringBuffer字符串是否相等
        StringBuffer strBuffer3 = new StringBuffer("ni Hao, world!");
        boolean flag = strBuffer1.toString().equals(strBuffer3.toString());
        System.out.println("strBuffer1 == strBuffer3 : "+flag);//strBuffer1 == strBuffer3 : true
        // 遍歷StringBuffer,替換’o‘ -> 'O'
        for (int i = 0 ; i < strBuffer3.length() ; i++) {
            if (strBuffer3.charAt(i) == 'o') {
                strBuffer3.replace(i, i+1, "O");
            }
        }
        System.out.println("strBuffer3 = "+strBuffer3);//strBuffer3 = ni HaO, wOrld!

4.StringBuilder

StringBuffer是線程安全的,StringBuilder不是線程安全的,方法基本一樣

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