亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

一文帶你初識java中的String類

 更新時間:2021年10月24日 09:09:39   作者:文墨軒  
String代表字符串,Java 程序中的所有字符串字面值(如 "abc" )都作為此類的實例實現,這篇文章主要給大家介紹了關于java中String類的相關資料,需要的朋友可以參考下

什么是字符串

字符串或串(String)是由數字、字母、下劃線組成的一串字符。一般記為 s=“a1a2···an”(n>=0)。它是編程語言中表示文本的數據類型。在程序設計中,字符串(string)為符號或數值的一個連續(xù)序列,如符號串(一串字符)或二進制數字串(一串二進制數字)。其在java語言中可以通過一定的方法提取字符串中的一個字符

字符串常見的賦值方法

直接賦值法

String 變量名=" 初始值"

這種賦值方法經常被我們使用

 public static void main(String[] args) {
        String str1="hello world";
        System.out.println(str1);
    }

在這里插入圖片描述

構造方法進行創(chuàng)建

格式:

String 變量名=new String(初始值)

public static void main(String[] args) {
        String str2=new String("SWPU YYDS");
        System.out.println(str2);
    }

在這里插入圖片描述

注意:初始值不只局限于常量字符串,也可以是數組

比如字符數組:

public static void main(String[] args) {
        char ch[]={'S','W','P','U'};
        String str3=new String(ch);
        System.out.println(str3);
    }

在這里插入圖片描述

又如字節(jié)數組:

public static void main(String[] args) {
        byte a[]={94,95,94};
        String str3=new String(a);
        System.out.println(str3);
    }

在這里插入圖片描述

此外也可以通過這種構造方式把字符數組或者字節(jié)數組部分元素轉換為字符串

public static void main(String[] args) {
        char ch[]={'a','b','c','d','e','f'};
        String str4=new String(ch,1,3);//1這個位置是字符串的開始位置,3為字符串的長度
        System.out.println(str4);
    }

在這里插入圖片描述

字符串的比較相等

在int變量中判斷兩個int變量被賦予的值是否相等使用“==”便可以判斷

 public static void main(String[] args) {
        int a=520;
        int b=520;
        System.out.println("a==b:"+(a==b));
    }

在這里插入圖片描述

那么有人就會認為字符串的比較也可以用“==”進行比較

如圖:

public static void main(String[] args) {
        String str5="abcdef";
        String str6="abcdef";
        System.out.println("str5和str6是否相等"+(str5==str6));
    }

在這里插入圖片描述

那么字符串是不是就依靠“==”進行判斷的呢?其結果顯然是

在這里插入圖片描述


就如這個代碼:

public static void main(String[] args) {
        String str8="abcdef";
        String str7=new String("abcdef");
        System.out.println("str7和str8是否相等"+(str7==str8));
    }

在這里插入圖片描述

 public static void main(String[] args) {
        String str8=new String("abcdef");
        String str7=new String("abcdef");
        System.out.println("str7和str8是否相等"+(str7==str8));
    }

在這里插入圖片描述

為什么使用"=="會出現這種情況呢?

實際上String 使用 == 比較并不是在比較字符串內容, 而是比較兩個引用是否是指向同一個對象

就比如:這里有兩個籃子分別為A,B;分別里面放相同的5個蘋果,將這兩個籃子看做兩個不同的類A和類B,而這個比較的便是類是否相同,而不是比較類里面的內容是否相同。

為了比較字符串內容是否相等那么我們就得使用String類提供的equals方法

public static void main(String[] args) {
        String str8="abcdef";
        String str7=new String("abcdef");
        System.out.println("str7和str8是否相等"+(str7.equals(str8)));
    }

在這里插入圖片描述

public static void main(String[] args) {
        String str8=new String("abcdef");
        String str7=new String("abcdef");
        System.out.println("str7和str8是否相等"+(str7.equals(str8)));
    }

在這里插入圖片描述

字符串常量池

在上面所講的字符串賦值操作中

直接賦值為什么使用“==”進行比較有的就是true而有的就是false呢?

實際上當我們采用了直接賦值的模式進行String類的對象實例化操作,那么該實例化對象(字符串內容)將自動保存到字符串常量池之中,并且如果下次繼續(xù)使用直接賦值的模式聲明String類對象,此時對象池之中如若有指定內容,將直接進行引用如若沒有,則開辟新的字符串對象而后將其保存在對象池之中以供下次使用。

public static void main5(String[] args) {
        String str5="abcdef";
        String str6="abcdef";
        System.out.println("str5和str6是否相等"+(str5==str6));
    }

在這里插入圖片描述

而我們使用構造方法時,首先String類的對象實例化操作時,先在字符常量池中尋找是否有字符串內容,如果有就不需要在放入字符串常量池中,其次在堆上開辟一個內存空間,該空間指向字符串常量池上的內容,而棧上的對象指向新開辟的內存空間

public static void main6(String[] args) {
        String str8="abcdef";
        String str7=new String("abcdef");
        System.out.println("str7和str8是否相等"+(str7==str8));
    }

在這里插入圖片描述

在這里插入圖片描述

注意:在采用直接賦值時,只會開辟一塊堆內存空間,并且該字符串對象可以自動保存在對象池中以供下次使用。在采用構造方法時會開辟兩塊堆內存空間,不會自動保存在對象池中,可以使用intern()方法手工池。

字符串常量池的實例

為了更好了解字符串常量池這里有幾個實例可以加以練習

實例一

public static void main(String[] args) {
        String str1="abcdef";
        String str2="abc"+"def";
        System.out.println(str1==str2);
    }

在這里插入圖片描述

在這里插入圖片描述

2. 實例二

 public static void main(String[] args) {
        String str1="abcdef";
        String str2="abc";
        String str3=str2+"def";
        System.out.println(str1==str3);
    }

注意str2是變量,在編譯的時候無法知道里面的值是什么,只有運行到時才知道

在這里插入圖片描述

在這里插入圖片描述

3. 實例三

public static void main(String[] args) {
        String str1="abcdef";
        String str2="abc"+new String("def");
        System.out.println(str1==str2);
    }

在這里插入圖片描述

在這里插入圖片描述

4. 實例四

public static void main(String[] args) {
        String str1="abcdef";
        String str2=new String("abc")+new String("def");
        System.out.println(str1==str2);
    }

在這里插入圖片描述

在這里插入圖片描述

5. 實例五

public static void main(String[] args) {
        String str1="abc";
        String str2=str1;
        str1="def";
        System.out.println(str1==str2);
    }

在這里插入圖片描述

在這里插入圖片描述

字符串的不可變

字符串是一種不可變對象. 它的內容不可改變.

String 類的內部實現也是基于 char[] 來實現的, 但是 String 類并沒有提供 set 方法之類的來修改內部的字符數組.

如代碼:

public static void main(String[] args) {
        String str = "hello" ;
        str = str + " world" ;
        str += "!!!" ;
        System.out.println(str);
    }

在這里插入圖片描述

結果看似簡單其實底層進行了許多操作

第一步:

在這里插入圖片描述

第二步:

在這里插入圖片描述

第三步:

在這里插入圖片描述

那么如果我們需要修改字符串, 例如, 現有字符串 str = “Hello” , 想改成 str = “hello” , 該怎么辦?

最簡單的方法是:借助原字符串,創(chuàng)建新的字符串

public static void main(String[] args) {
        String str = "Hello";
        str = "h" + str.substring(1);
        System.out.println(str);
    }

在這里插入圖片描述

為什么String為不可變?

  1. 方便實現字符串對象池. 如果 String 可變, 那么對象池就需要考慮何時深拷貝字符串的問題了。
  2. 不可變對象是線程安全的。
  3. 不可變對象更方便緩存 hash code, 作為 key 時可以更高效的保存到 HashMap 中。

字符串的常見操作

字符串的比較

equals方法

該方法比較字符串的內容并區(qū)分字符的大小關系

public static void main(String[] args) {
        String str="Hello";
        String str1="hello";
        System.out.println(str.equals(str1));
    }

在這里插入圖片描述

equalsIgnoreCase方法

該方法比較字符串的內容但不區(qū)分字符的大小關系

public static void main(String[] args) {
        String str="Hello";
        String str1="hello";
        System.out.println(str.equalsIgnoreCase(str1));
    }

在這里插入圖片描述

compareTo方法

在String類中compareTo()方法是一個非常重要的方法,該方法返回一個整型,該數據會根據大小關系返回三類內容:如果相等則返回0;如果小于比較的字符串則返回小于0;相反則返回大于0的值;

 public static void main(String[] args) {
        String str1="Hello";
        String str2="hello";
        System.out.println(str1.compareTo(str2));
    }

在這里插入圖片描述

public static void main(String[] args) {
        String str2="Hello";
        String str1="hello";
        System.out.println(str1.compareTo(str2));
    }

在這里插入圖片描述

 public static void main(String[] args) {
        String str2="hello";
        String str1="hello";
        System.out.println(str1.compareTo(str2));
    }

在這里插入圖片描述

字符串的查找

contains()方法

該方法可以判斷該字符串是否有子串有則返回true;沒有就返回false;

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="xass";
        System.out.println(str1.contains(str2));
    }

在這里插入圖片描述

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="acbaskj";
        System.out.println(str1.contains(str2));
    }

在這里插入圖片描述

indexOf()方法

該方法使字符串從頭開始查找需要查找字符串的位置,查到了就返回位置的開始索引,如果沒有檢查到則返回-1。

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="askj";
        System.out.println(str1.indexOf(str2));
    }

在這里插入圖片描述

 public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="askjsass";
        System.out.println(str1.indexOf(str2));
    }

在這里插入圖片描述

lastIndexOf(String str)方法

由后向前查找子字符串

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="askj";
        System.out.println(str1.lastIndexOf(str2));
    }

在這里插入圖片描述

lastIndexOf(String str,int fromIndex)方法

從指定位置由后向前查找。

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="askj";
        System.out.println(str1.lastIndexOf(str2,1));
    }

在這里插入圖片描述

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="askj";
        System.out.println(str1.lastIndexOf(str2,13));
    }

在這里插入圖片描述

startsWith(String prefix)

判斷是否以指定的字符串開頭是則返回true不是則返回false

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="askj";
        System.out.println(str1.startsWith(str2));
    }

在這里插入圖片描述

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="ac";
        System.out.println(str1.startsWith(str2));
    }

在這里插入圖片描述

startsWith(String prefix,int toffset)

從指定位置開始判斷是否以指定字符串開頭

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="bask";
        System.out.println(str1.startsWith(str2,2));
    }

在這里插入圖片描述

endsWith(String suffix)

判斷是否以指定字符串結尾

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="bask";
        System.out.println(str1.endsWith(str2));
    }

在這里插入圖片描述

public static void main(String[] args) {
        String str1="acbaskjcbaskd";
        String str2="skd";
        System.out.println(str1.endsWith(str2));
    }

在這里插入圖片描述

字符串替換

replaceAll(String regx,String replacement)

替換字符串中所有指定的內容

public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.replaceAll("l", "_"));
    }

在這里插入圖片描述

replaceFirst(String regx,String replacement)

替換字符串中第一個指定的內容

 public static void main(String[] args) {
        String str = "helloworld" ;
        System.out.println(str.replaceFirst("l", "_"));
    }

在這里插入圖片描述

注意: 由于字符串是不可變對象, 替換不修改當前字符串, 而是產生一個新的字符串.

字符串拆分

split(String regex)

將字符串全部拆分

public static void main(String[] args) {
        String str = "hello world hello swpu" ;
        String[] result = str.split(" ") ;
        for(String s: result) {
            System.out.println(s);
        }
    }

在這里插入圖片描述

注意:有些特別的字符作為分隔符可能無法正確切分需要加上轉義

如:

public static void main(String[] args) {
        String str = "192.168.1.1" ;
        String[] result = str.split("\\.") ;
        for(String s: result) {
            System.out.println(s);
        }
    }

在這里插入圖片描述

注意:

字符"|","*","+“都得加上轉義字符,前面加上”\".而如果是".",那么就得寫成"\".如果一個字符串中有多個分隔符,可以用"|"作為連字符 split(String regex,int limit)

將字符串部分拆分,該數組長度就是limit極限

public static void main(String[] args) {
        String str = "hello world hello swpu" ;
        String[] result = str.split(" ",3) ;
        for(String s: result) {
            System.out.println(s);
        }
    }

在這里插入圖片描述

字符串截取

substring(int bedinIndex)

從指定索引截止到結尾

public static void main(String[] args) {
        String str="helloworld";
        System.out.println(str.substring(3));
    }

在這里插入圖片描述

substring(int beginIndex,int endIndex)

截取字符串的部分內容

public static void main(String[] args) {
        String str="helloworld";
        System.out.println(str.substring(3,5));
    }

在這里插入圖片描述

注意:該區(qū)間為左閉右開則:[3,5)不包含5下標

總結

到此這篇關于java中String類的文章就介紹到這了,更多相關java的String類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Mybatis-Plus實現自動生成代碼的操作步驟

    Mybatis-Plus實現自動生成代碼的操作步驟

    AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個模塊的代碼,極大的提升了開發(fā)效率,本文將給大家介紹Mybatis-Plus實現自動生成代碼的操作步驟
    2023-10-10
  • SpringBoot搭建go-cqhttp機器人的方法實現

    SpringBoot搭建go-cqhttp機器人的方法實現

    本文主要介紹了SpringBoot搭建go-cqhttp機器人的方法實現
    2021-12-12
  • 2021年最新Redis面試題匯總(4)

    2021年最新Redis面試題匯總(4)

    在程序員面試過程中redis相關的知識是常被問到的話題。這篇文章主要介紹了幾道Redis面試題,整理一下分享給大家,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 深入理解Java設計模式之享元模式

    深入理解Java設計模式之享元模式

    這篇文章主要介紹了JAVA設計模式之享元模式的的相關資料,文中示例代碼非常詳細,供大家參考和學習,感興趣的朋友可以了解下
    2021-11-11
  • 如何把Spring Cloud Data Flow部署在Kubernetes上

    如何把Spring Cloud Data Flow部署在Kubernetes上

    這篇文章主要介紹了把Spring Cloud Data Flow部署在Kubernetes上,再跑個任務試試,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • HashMap工作原理_動力節(jié)點Java學院整理

    HashMap工作原理_動力節(jié)點Java學院整理

    這篇文章主要介紹了HashMap工作原理_動力節(jié)點Java學院整理,需要的朋友可以參考下
    2017-04-04
  • 高效數據傳輸的秘密武器Protobuf的使用教程

    高效數據傳輸的秘密武器Protobuf的使用教程

    Protobuf(Protocol?Buffers)是由?Google?開發(fā)的一種輕量級、高效的數據交換格式,它被用于結構化數據的序列化、反序列化和傳輸,本文主要介紹了它的具體使用方法,需要的可以參考一下
    2023-05-05
  • 淺談@Value和@Bean的執(zhí)行順序問題

    淺談@Value和@Bean的執(zhí)行順序問題

    這篇文章主要介紹了@Value和@Bean的執(zhí)行順序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java開發(fā)中的OOM內存溢出問題詳解

    Java開發(fā)中的OOM內存溢出問題詳解

    這篇文章主要介紹了Java開發(fā)中的OOM內存溢出問題詳解,OOM,全稱?Out?Of?Memory,意思是內存耗盡或內存溢出,當JVM因為沒有足夠的內存來為對象分配空間并且垃圾回收器也已經沒有空間可回收時,就會拋出這個?error,需要的朋友可以參考下
    2023-08-08
  • Springboot項目中單元測試時注入bean失敗的解決方案

    Springboot項目中單元測試時注入bean失敗的解決方案

    這篇文章主要介紹了Springboot項目中單元測試時注入bean失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評論