欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文帶你初識java中的String類

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

什么是字符串

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

字符串常見的賦值方法

直接賦值法

String 變量名=" 初始值"

這種賦值方法經(jīng)常被我們使用

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

在這里插入圖片描述

構(gòu)造方法進(jìn)行創(chuàng)建

格式:

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

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

在這里插入圖片描述

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

比如字符數(shù)組:

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

在這里插入圖片描述

又如字節(jié)數(shù)組:

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

在這里插入圖片描述

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

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

在這里插入圖片描述

字符串的比較相等

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

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

在這里插入圖片描述

那么有人就會(huì)認(rèn)為字符串的比較也可以用“==”進(jìn)行比較

如圖:

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

在這里插入圖片描述

那么字符串是不是就依靠“==”進(jìn)行判斷的呢?其結(jié)果顯然是

在這里插入圖片描述


就如這個(gè)代碼:

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));
    }

在這里插入圖片描述

為什么使用"=="會(huì)出現(xiàn)這種情況呢?

實(shí)際上String 使用 == 比較并不是在比較字符串內(nèi)容, 而是比較兩個(gè)引用是否是指向同一個(gè)對象

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

為了比較字符串內(nèi)容是否相等那么我們就得使用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)));
    }

在這里插入圖片描述

字符串常量池

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

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

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

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

在這里插入圖片描述

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

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

在這里插入圖片描述

在這里插入圖片描述

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

字符串常量池的實(shí)例

為了更好了解字符串常量池這里有幾個(gè)實(shí)例可以加以練習(xí)

實(shí)例一

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

在這里插入圖片描述

在這里插入圖片描述

2. 實(shí)例二

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

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

在這里插入圖片描述

在這里插入圖片描述

3. 實(shí)例三

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

在這里插入圖片描述

在這里插入圖片描述

4. 實(shí)例四

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

在這里插入圖片描述

在這里插入圖片描述

5. 實(shí)例五

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

在這里插入圖片描述

在這里插入圖片描述

字符串的不可變

字符串是一種不可變對象. 它的內(nèi)容不可改變.

String 類的內(nèi)部實(shí)現(xiàn)也是基于 char[] 來實(shí)現(xiàn)的, 但是 String 類并沒有提供 set 方法之類的來修改內(nèi)部的字符數(shù)組.

如代碼:

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

在這里插入圖片描述

結(jié)果看似簡單其實(shí)底層進(jìn)行了許多操作

第一步:

在這里插入圖片描述

第二步:

在這里插入圖片描述

第三步:

在這里插入圖片描述

那么如果我們需要修改字符串, 例如, 現(xiàn)有字符串 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. 方便實(shí)現(xiàn)字符串對象池. 如果 String 可變, 那么對象池就需要考慮何時(shí)深拷貝字符串的問題了。
  2. 不可變對象是線程安全的。
  3. 不可變對象更方便緩存 hash code, 作為 key 時(shí)可以更高效的保存到 HashMap 中。

字符串的常見操作

字符串的比較

equals方法

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

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

在這里插入圖片描述

equalsIgnoreCase方法

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

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

在這里插入圖片描述

compareTo方法

在String類中compareTo()方法是一個(gè)非常重要的方法,該方法返回一個(gè)整型,該數(shù)據(jù)會(huì)根據(jù)大小關(guān)系返回三類內(nèi)容:如果相等則返回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)

判斷是否以指定字符串結(jié)尾

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)

替換字符串中所有指定的內(nèi)容

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

在這里插入圖片描述

replaceFirst(String regx,String replacement)

替換字符串中第一個(gè)指定的內(nèi)容

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

在這里插入圖片描述

注意: 由于字符串是不可變對象, 替換不修改當(dāng)前字符串, 而是產(chǎn)生一個(gè)新的字符串.

字符串拆分

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);
        }
    }

在這里插入圖片描述

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

如:

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

在這里插入圖片描述

注意:

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

將字符串部分拆分,該數(shù)組長度就是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)

從指定索引截止到結(jié)尾

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

在這里插入圖片描述

substring(int beginIndex,int endIndex)

截取字符串的部分內(nèi)容

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

在這里插入圖片描述

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

總結(jié)

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

相關(guān)文章

  • Mybatis-Plus實(shí)現(xiàn)自動(dòng)生成代碼的操作步驟

    Mybatis-Plus實(shí)現(xiàn)自動(dòng)生成代碼的操作步驟

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

    SpringBoot搭建go-cqhttp機(jī)器人的方法實(shí)現(xiàn)

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

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

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

    深入理解Java設(shè)計(jì)模式之享元模式

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

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

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

    HashMap工作原理_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了HashMap工作原理_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下
    2017-04-04
  • 高效數(shù)據(jù)傳輸?shù)拿孛芪淦鱌rotobuf的使用教程

    高效數(shù)據(jù)傳輸?shù)拿孛芪淦鱌rotobuf的使用教程

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

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

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

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

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

    Springboot項(xiàng)目中單元測試時(shí)注入bean失敗的解決方案

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

最新評論