Java String字符串詳解

一、字符串String詳解

1晨汹、實例化

實例化String對象:直接賦值终议、使用關鍵字new來進行開辟空間

代碼
public class Test41 {
    public static void main(String[] args) {
        String str = "Hello";  //直接賦值
        System.out.println(str);
        //使用new關鍵字
        String str1 = new String("Hello1");
        System.out.println(str1);
    }
}
結果:
Hello
Hello1
String使用new關鍵字.jpg

由上圖可以看出虫埂,使用new關鍵字實例化對象祥山,在內(nèi)存中開辟了兩個空間用來存儲他們,其中一個是無用的掉伏,所以使用第一種直接賦值方式更合理一些缝呕,可以更省略一些空間。

2岖免、String的內(nèi)容比較

由以下代碼進行說明

代碼
public class Test42 {
    public static void main(String[] args) {
        int a = 10;
        int b = 10;
        System.out.println(a==b);
        
        String str = "Hello";
        String str1 = new String("Hello"); //開辟了兩個空間地址
        System.out.println(str==str1); //"==" 比較的是地址
        System.out.println(str.equals(str1)); //"equals"比較的是內(nèi)容
    }
}
結果:
true
false
true

3岳颇、字符串內(nèi)容不可更改

字符串.png
代碼
public class Test43 {
    public static void main(String[] args) {
        String str = "hello";
         String str1 = str + " world!";
         System.out.println(str1);
    }
}
結果:
hello world!

二照捡、String字符串常用方法

1颅湘、字符串長度

length()方法

代碼
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        System.out.println("str字符串的長度:"+str.length());
    }
}
結果:
str字符串的長度:10

2、字符串轉換數(shù)組

toCharArray()

代碼
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        // 字符串轉換成數(shù)組
        char data[] = str.toCharArray();
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i]+" ");
        }
    }
}
結果:
h e l l o w o r l d 

3栗精、從字符串中取出指定位置的字符

charAt()

代碼
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        // 從字符串中取出指定位置的字符
        System.out.println(str.charAt(7));
    }
}
結果:
r

4闯参、字符串與byte數(shù)組的轉換

getBytes()

代碼
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        //字符串與byte數(shù)組的轉換
        byte bytes[] = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(new String(bytes)+"\t");
        }
    }
}
結果:
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  

5、過濾字符串中存在的字符

indexOf()

代碼
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld@163.com";
        System.out.println("@字符所在的位置:"+str.indexOf("@"));
    }
}
結果:
@字符所在的位置:10

6悲立、去掉字符串的前后空格

trim()

代碼
public class Test44 {
    public static void main(String[] args) {
        String str = "  hello world";
        System.out.println(str);
        System.out.println(str.trim());
    }
}
結果:
  hello world
hello world

7鹿寨、從字符串中取出子字符串

subString()

代碼
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        System.out.println(str.substring(4));
        System.out.println( str.substring(5));
    }
}
結果:
oworld
world

8、大小寫轉換

toLowerCase()薪夕、toUpperCase()

代碼
public class Test44 {
    public static void main(String[] args) {
        String str = "hello world";
        String str1 = "SEC";
        System.out.println(str.toUpperCase());
        System.out.println(str1.toLowerCase());
    }
}
結果:
HELLO WORLD
sec

9脚草、判斷字符串的開頭結尾字符

endsWith()startWith()

代碼
public class Test44 {
    public static void main(String[] args) {
        String email = "sec_hello@126.com";
        System.out.println(email.startsWith("sec"));
        System.out.println(email.endsWith("com"));
        System.out.println(email.endsWith("163.com"));
    }
}
結果:
true
true
false

10原献、替換String字符串中的一個字符

replace()

代碼
public class Test44 {
    public static void main(String[] args) {
        String email = "sec_hello@126.com";
        System.out.println("將字符串中的e替換成m:"+email.replace('e', 'm'));
    }
}
結果:
將字符串中的e替換成m:smc_hmllo@126.com

三馏慨、StringBuffer方法

1、認識StringBuffer

緩沖區(qū)姑隅,本身也是操作字符串写隶,但是與String不同,StringBuffer是可以更改的讲仰。
StringBuffer是一個操作類慕趴,所以必須通過實例化進行操作。

代碼
public class Test45 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello");
        System.out.println("StringBuffer更改前:"+sBuffer.toString());
        tell(sBuffer);
        System.out.println("StringBuffer更改后:"+sBuffer.toString());
        
        String string = "android";
        System.out.println("String更改前:"+string);
        tell1(string);
        System.out.println("String更改后:"+string);
    }
    
    public static void tell(StringBuffer s) {
        s.append(" I love sec");
    }
    
    public static void tell1(String str) {
        str = "java";
    }
}
結果:
StringBuffer更改前:hello
StringBuffer更改后:hello I love sec
String更改前:android
String更改后:android

2鄙陡、StringBuffer常用方法

append() 追加

示例
public class Test46 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello ");
        sBuffer.append("world");
        System.out.println(sBuffer.toString());
    }
}
結果:
hello world

insert() 插入

示例
public class Test46 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello ");
        sBuffer.append("world");
        System.out.println(sBuffer.toString());
        
        sBuffer.insert(6, "love "); //從第7個位置開始插入字符love 
        System.out.println(sBuffer.toString());
    }
}
結果:
hello world
hello love world

replace() 替換

示例
public class Test46 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello ");
        sBuffer.append("world");
        System.out.println(sBuffer.toString());
        sBuffer.replace(1, 4, "wwtcom");//從第2個位置到第4個位置替換成"wwtcom"
        System.out.println(sBuffer.toString());
    }
}
結果:
hello world
hwwtcomo world

indexOf() 字符串存在的位置

示例
public class Test46 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello@");
        sBuffer.append("world");
        System.out.println(sBuffer.toString());
        System.out.println("@字符存在的位置:"+sBuffer.indexOf("@"));
    }
}
結果:
hello@world
@字符存在的位置:5

3冕房、StringBuffer類的應用

代碼
public class Test47 {
    public static void main(String[] args) {
        String str = "hello";
        for (int i = 0; i < 100; i++) {
            str = str + i;
        }
        System.out.println(str); //使用String需要開辟101個空間,很耗資源
        
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello");
        for (int i = 0; i < 100; i++) {
            sBuffer.append(i);
        }
        System.out.println(sBuffer.toString());//使用StringBuffer運行很快趁矾,不需要重新開辟空間
    }
}
結果:
hello0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
hello0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

四耙册、StringBuider用法

  • 一個可變的字符序列,該類被設計作用StringBuffer的一個簡易替換愈魏,用在字符串緩沖區(qū)被單個線程使用的時候觅玻。建議優(yōu)先考慮該類想际,速度比StringBuffer要快。
  • 但是如果設計到線程安全溪厘,建議使用StringBuffer胡本。
  • 常用方法:append()insert() 使用方式與StringBuffer是一樣的畸悬。
代碼
public class Test48 {
    public static void main(String[] args) {
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append("Hello ");
        sBuilder.append("world");
        System.out.println("sBuilder更改前:"+sBuilder.toString());
        
        sBuilder.insert(6, "love "); //從第7個位置開始插入字符love 
        System.out.println("sBuilder更改后:"+sBuilder.toString());
    }
}
結果:
sBuilder更改前:Hello world
sBuilder更改后:Hello love world
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末侧甫,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蹋宦,更是在濱河造成了極大的恐慌披粟,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冷冗,死亡現(xiàn)場離奇詭異守屉,居然都是意外死亡,警方通過查閱死者的電腦和手機蒿辙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門拇泛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人思灌,你說我怎么就攤上這事俺叭。” “怎么了泰偿?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵熄守,是天一觀的道長。 經(jīng)常有香客問我耗跛,道長裕照,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任课兄,我火速辦了婚禮牍氛,結果婚禮上,老公的妹妹穿的比我還像新娘烟阐。我一直安慰自己搬俊,他們只是感情好,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布蜒茄。 她就那樣靜靜地躺著唉擂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪檀葛。 梳的紋絲不亂的頭發(fā)上玩祟,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機與錄音屿聋,去河邊找鬼空扎。 笑死藏鹊,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的转锈。 我是一名探鬼主播盘寡,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼撮慨!你這毒婦竟也來了竿痰?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤砌溺,失蹤者是張志新(化名)和其女友劉穎影涉,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體规伐,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡蟹倾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了楷力。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片喊式。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖萧朝,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情夏哭,我是刑警寧澤检柬,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站竖配,受9級特大地震影響何址,放射性物質發(fā)生泄漏。R本人自食惡果不足惜进胯,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一用爪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧胁镐,春花似錦偎血、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至就缆,卻和暖如春帖渠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背竭宰。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工空郊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留份招,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓狞甚,卻偏偏與公主長得像脾还,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子入愧,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容

  • java中String的常用方法 1鄙漏、length()字符串的長度 例:char chars[]={'a','b'...
    赤赤有名閱讀 2,053評論 0 10
  • 前面我們總結了數(shù)組操作,這里我們將總結字符串相關的知識棺蛛,除了總結String的API用法怔蚌,同時我們還會總結一些相關...
    HCherisher閱讀 3,621評論 2 6
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法旁赊,內(nèi)部類的語法桦踊,繼承相關的語法,異常的語法终畅,線程的語...
    子非魚_t_閱讀 31,632評論 18 399
  • 一籍胯、String 類 1、定義: 1离福、從概念上講杖狼,java字符串就是Unicode字符序列。每個用雙引號括起來的字...
    玉圣閱讀 1,577評論 0 1
  • 構造方法的重載 構造方法的重載是指在同一個類中存在著若干個具有不同參數(shù)列表的構造方法妖爷。有時在一個類定義內(nèi)可能出現(xiàn)多...
    陳老板_閱讀 312評論 0 0