Java常用類-String類

来源:https://www.cnblogs.com/mengd/archive/2020/07/14/13301810.html
-Advertisement-
Play Games

字元串相關的類:String String類:代表字元串,Java 程式中的所有字元串字面值(如 "abc" )都作 為此類的實例實現。 String是一個final類,代表不可變的字元序列 當字元串重新賦值時,需要重新指定記憶體區域,不能使用原有的value進行賦值 當對現有的字元串進行連接操作時, ...


字元串相關的類:String

String類:代表字元串,Java 程式中的所有字元串字面值(如 "abc" )都作 為此類的實例實現。

  • String是一個final類,代表不可變的字元序列
    • 當字元串重新賦值時,需要重新指定記憶體區域,不能使用原有的value進行賦值
    • 當對現有的字元串進行連接操作時,也需要重新指定記憶體區域賦值,也不能 在原有的value上進行賦值
    • 當調用String的replace()方法修改指定字元或字元串時,也需要重新指定記憶體區域進行賦值
    • 也就是說對String的任何修改都是重新的造一個
  • 字元串是常量,用雙引號引起來表示。它們的值在創建之後不能更改
  • String內部定義了final char value[] 用於存儲字元串數據
  • String實現了Serializable介面,表示字元串是支持序列化的,實現了 Comparable介面,表示String可以比較大小
  • 通過字面量的方式(區別於new)給一個字元串賦值,此時的字元串的值是聲明在字元串常量池中的
  • 字元串常量池是不會存儲相同內容的字元串
public void test1(){
        String s1 = "abc";//字面量的方式
        String s2 = "abc";

        System.out.println(s1 == s2);// ture , 比較的是s1和s2的地址值
        s1 = "hello";

        System.out.println(s1); // hello
        System.out.println(s2); // abc

        String s3 = "abc";
        s3 += "def";
        System.out.println(s3); // abcdef
        System.out.println(s2 == s3); // false , 此時的s2和s3的地址值已經不同


        String s4 = "abc";
        String s5 = s4.replace('a' , 'd');
        System.out.println(s4); // abc
        System.out.println(s5); // dbc
    }

1. String的實例化方式

  • 通過字面量定義的方式:此時的s1和s2的數據是聲明在方法區的字元串常量池中
  • 通過new + 構造器的方式:此時的s3和s4保存的地址,是數據在堆空間中開闢空間以後對應的地址值
  public void test2(){

        String s1 = "Java";
        String s2 = "Java";

        String s3 = new String("Java");
        String s4 = new String("Java");

        System.out.println(s1 == s2); // true
        System.out.println(s1 == s3); // false
        System.out.println(s1 == s4); // false
        System.out.println(s3 == s4); // false



        Person p1 = new Person("Tom",18);
        Person p2 = new Person("Tom",18);
        System.out.println(p1.name.equals(p2.name)); // true , String重寫了equals方法,此時比較的就是內容了
        System.out.println(p1.name == p2.name); // true , 兩個對象裡面還是字面量定義的方式賦值
    }

註:兩個對象裡面還是字面量定義的方式賦值

面試題

String s = new String("abc")方式創建對象,在記憶體中創建了幾個對象?

兩個,一個是堆空間的new的結構,一個是char[]對應的常量池中的數據 "abc"

2. 字元串的特性

重點:

  • 常量與常量的拼接結果在常量池中,且常量池中不會存在相同內容的常量
  • 只要其中一個是變數,結果就在堆中
  • 如果拼接的結果調用intern()方法,返回值就是在常量池中

public void test3(){
        String s1 = "Java";
        String s2 = "Python";

        String s3 = "JavaPython";
        String s4 = "Java" + "Python";
        String s5 = s1 + "Python";
        String s6 = s1 + s2;

        System.out.println(s3 == s4); // true

        System.out.println(s3 == s5); // false
        System.out.println(s4 == s5); // false
        System.out.println(s3 == s6); // false

        String s7 = s5.intern();
        System.out.println(s7 == s3); //true
    
    }

特別註意:當 final String s1 = “java”,這個也就相當於一個常量了

3. 面試題:

good and best

這裡可以參考之前的Java的值傳遞

若在scr前面加一個this,情況會是怎樣?

package com.atguigui.exer;

/**
 * @author MD
 * @create 2020-07-12 9:41
 */
public class StringTest {

    String str = new String("good");
    char[] ch = {'t','e','s','t'};

    public void change(String str, char ch[]){
        this.str = "test ok";
        ch[0] = 'b';
    }

    public static void main(String[] args) {
        StringTest ex = new StringTest();
        ex.change(ex.str,ex.ch);
        System.out.println(ex.str);
        System.out.println(ex.ch);
        // test ok
        // best
        
    }
}

4. String常用的方法

註意String的不可變性,原字元不變

  • int length():返回字元串的長度: return value.length
  • char charAt(int index): 返回某索引處的字元return value[index]
  • boolean isEmpty():判斷是否是空字元串:return value.length == 0
  • String toLowerCase():使用預設語言環境,將 String 中的所有字元轉換為小寫
  • String toUpperCase():使用預設語言環境,將 String 中的所有字元轉換為大寫
  • String trim():返回字元串的副本,忽略前導空白和尾部空白
  • boolean equals(Object obj):比較字元串的內容是否相同
  • boolean equalsIgnoreCase(String anotherString):與equals方法類似,忽略大 小寫
  • String concat(String str):將指定字元串連接到此字元串的結尾。 等價於用“+”
  • int compareTo(String anotherString):比較兩個字元串的大小 涉及到字元串排序
  • String substring(int beginIndex):返回一個新的字元串,它是此字元串的從 beginIndex開始截取到最後的一個子字元串。
  • String substring(int beginIndex, int endIndex) :返回一個新字元串,它是此字 符串從beginIndex開始截取到endIndex(不包含)的一個子字元串。
  • boolean endsWith(String suffix):測試此字元串是否以指定的尾碼結束
  • boolean startsWith(String prefix):測試此字元串是否以指定的首碼開始
  • boolean startsWith(String prefix, int toffset):測試此字元串從指定索引開始的 子字元串是否以指定首碼開始
  • boolean contains(CharSequence s):當且僅當此字元串包含指定的 char 值序列 時,返回 true,也就是字元串A中是否包含字元串B
  • int indexOf(String str):返回指定子字元串在此字元串中第一次出現處的索引
  • int indexOf(String str, int fromIndex):返回指定子字元串在此字元串中第一次出 現處的索引,從指定的索引開始
  • int lastIndexOf(String str):返回指定子字元串在此字元串中最右邊出現處的索引
  • int lastIndexOf(String str, int fromIndex):返回指定子字元串在此字元串中最後 一次出現處的索引,從指定的索引開始反向搜索
  • 註:indexOf和lastIndexOf方法如果未找到都是返回-1
  • String replace(char oldChar, char newChar):返回一個新的字元串,它是 通過用 newChar 替換此字元串中出現的所有 oldChar 得到的。
  • String replace(CharSequence target, CharSequence replacement):使 用指定的字面值替換序列替換此字元串所有匹配字面值目標序列的子字元串。
  • String replaceAll(String regex, String replacement) : 使用給 定 的 replacement 替換此字元串所有匹配給定的正則表達式的子字元串。
  • String replaceFirst(String regex, String replacement) : 使用給 定 的 replacement 替換此字元串匹配給定的正則表達式的第一個子字元串。
  • boolean matches(String regex):告知此字元串是否匹配給定的正則表達式
  • String[] split(String regex):根據給定正則表達式的匹配拆分此字元串。
  • String[] split(String regex, int limit):根據匹配給定的正則表達式來拆分此 字元串,最多不超過limit個,如果超過了,剩下的全部都放到最後一個元素中。

5. String與基本數據類型轉換

String ---> 基本數據類型、包裝類

  • 調用包裝類的靜態方法:parseXxx(str)

基本數據類型、包裝類 --- >字元串

  • 調用String的valueOf(xxx)
public void test2(){
        String str = "123";
        
        int num = Integer.parseInt(str);
        
        String str1 = String.valueOf(num);
    }

String ---> char[] : 調用String的toCharArray()

char[] ---> String: 調用String的構造器

public void test3(){
        String str = "asdf123";

        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            System.out.println(charArray[i]);
        }

        String str1 = new String(charArray);
        System.out.println(str1);

    }

編碼:String ---> byte[] :調用String的getBytes()
解碼:byte[] ---> String : 調用String的構造器

/**
     * 編碼:字元串 -->位元組 (看得懂的---->看不懂的二進位數據)
     * 解碼:位元組 ---> 字元串(看不懂的二進位數據---->看得懂的)
     * @throws UnsupportedEncodingException
     */
    @Test
    public void test4() throws UnsupportedEncodingException {
        String str = "abc123你好";
        byte[] bytes = str.getBytes(); // 使用預設的字元集進行轉換 UTF-8 編碼
        System.out.println(Arrays.toString(bytes));

        String str1 = "abc123你好";
        byte[] bytes1 = str1.getBytes("gbk"); // 指定編碼格式
        System.out.println(Arrays.toString(bytes1));


        System.out.println("---------------------");
        String str2 = new String(bytes); // 解碼,使用的預設的格式
        System.out.println(str2);

        String str3 = new String(bytes1 , "gbk");// 解碼,使用的指定的格式
        System.out.println(str3);
    }

6. StringBuffer、StringBuilder ?

面試題:對比String、StringBuffer、StringBuilder ?

  • String(JDK1.0):不可變字元序列 ,效率最低
  • StringBuffer(JDK1.0):可變字元序列、效率低、線程安全 同步
  • StringBuilder(JDK 5.0):可變字元序列、效率高、線程不安全
  • 底層都使用char[]存儲

註意:作為參數傳遞的話,方法內部String不會改變其值,StringBuffer和StringBuilder 會改變其值

源碼分析:
String str = new String(); // char[] value = new char[0];
String str1 = new String("abc"); // char[] value = new char[]{'a','b','c'};

StringBuffer sb1 = new StringBuffer(); // char[] value = new char[16];底層創建一個長度16的char型數組
StringBuffer sb1 = new StringBuffer("abc"); // char[] value = new char["abc".length()+16];

問題1:

註意這個是裡面 裡面數據的長度,

public void test5(){
        StringBuffer sb1 = new StringBuffer();
        System.out.println(sb1.length()); // 0

        StringBuilder sb2 = new StringBuilder("abc");
        System.out.println(sb2.length()); // 3

    }

問題2:

擴容問題,如果要添加的數據放不下了,那就需要擴容數組,預設情況下,擴容為原來的2倍+2,將原有數組中的數據複製到新的數組中去

String -----> StringBuffer、StringBuilder : 調用StringBuffer、StringBuilder構造器就可以了

StringBuffer、StringBuilder -----> : 調用String的構造器

StringBuffer類的常用方法和StringBuilder類似

  • StringBuffer append(xxx):提供了很多的append()方法,用於進行字元串拼接
  • StringBuffer delete(int start,int end):刪除指定位置的內容
  • StringBuffer replace(int start, int end, String str):把[start,end)位置替換為str
  • StringBuffer insert(int offset, xxx):在指定位置插入xxx
  • StringBuffer reverse() :把當前字元序列逆轉
  • public int indexOf(String str)
  • public String substring(int start,int end)
  • public int length()
  • public char charAt(int n )
  • public void setCharAt(int n ,char ch)

總結:

  • 增 :append
  • 刪:delete
  • 改:setCharAt,replace
  • 查:charAt
  • 插入:insert
  • 長度:length

面試題:

 public void testStringBuffer(){
        String str = null;
        StringBuffer sb = new StringBuffer();
        sb.append(str);

        System.out.println(sb.length()); // 4

        System.out.println(sb); //  "null" 這個是字元串null

        StringBuffer sb1 = new StringBuffer(str); // 拋異常 NullPointerException
        System.out.println(sb1);
    }

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 用形狀、大小完全相同的一種或幾種平面圖形進行拼接,彼此之間不留空隙、不重疊地鋪成一片,就叫做這幾種圖形的平面鑲嵌。 1.用一種多邊形實現的平面鑲嵌圖案 我們可以採用正三角形、正方形或正六邊形實現平面鑲嵌。 (1)用正方形平鋪。 用正方形進行平面鑲嵌比較簡單,編寫如下的HTML代碼。 <!DOCTYP ...
  • Properties標簽 第一種:全局配置文件內部配置數據源信息 (1)在全局配置文件中編寫數據源信息 <properties> <!--name指定數據源名稱,value指定其值--> <property name="driver" value="com.mysql.jdbc.Driver"/> ...
  • 生產者-消費者算是併發編程中常見的問題。依靠緩衝區我們可以實現生產者與消費者之間的解耦。生產者只管往緩衝區裡面放東西,消費者只管往緩衝區裡面拿東西。這樣我們避免生產者想要交付數據給消費者,但消費者此時還無法接受數據這樣的情況發生。 wait notify 這個問題其實就是線程間的通訊,所以要註意的是 ...
  • Django框架的基本使用 Django是一個功能強大的web框架 框架模式 1、MVC和MTV框架 MVC:Web伺服器開發領域里著名的MVC模式,所謂MVC就是把Web應用分為模型(M),控制器(C)和視圖(V)三層,結構說明如下: M: models 資料庫相關操作 V: views 視圖,也 ...
  • import os import fnmatch ls=os.listdir(r"E:\pdf") #列出文件夾下所有的文件 for 文件名 in ls: if fnmatch.fnmatch(文件名,"[!a-z].pdf"): print(文件名) 1.pdf2.pdf3.pdf4.pdf>>> ...
  • 從接觸 Python 時起,我就覺得 Python 的元組解包(unpacking)挺有意思,非常簡潔好用。 最顯而易見的例子就是多重賦值,即在一條語句中同時給多個變數賦值: >>> x, y = 1, 2 >>> print(x, y) # 結果:1 2 在此例中,賦值操作符“=”號的右側的兩個數 ...
  • 我們為什麼要使用泛型? 在學習集合時,我們都知道集合中是可以存放任意對象的,只要把對象存儲集合後,那麼這時他們都會被提升成Object類型。當我們在取出每一個對象,並且進行相應的操作,這時必須採用向下類型轉換。這個時候就要註意了,很容易出現問題. public class GenericDemo { ...
  • C語言簡介 C 語言是一種通用的高級語言,最初是由丹尼斯·里奇在貝爾實驗室為開發 UNIX 操作系統而設計的。C 語言最開始是於 1972 年在 DEC PDP-11 電腦上被首次實現。 原文鏈接:https://juejin.im/post/5df8c917f265da339772a5d1#he ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...