String字串及常用API

2021-09-25 22:46:12 字數 3481 閱讀 2137

string表示的是字串,通常用雙引號括起來"  "

從原始碼我們可以看出,string是乙個public final class 修飾的類,所以string不是基本資料型別,由final修飾的類為最終類,所以string不能被繼承

1.字面量宣告並初始化:     string 變數名="值";

string s1="helloword";
2.new方式

string s2 = new string(「helloworld」);
問:兩種方法建立的字串物件在記憶體上有什麼區別?

對於第一種字面量宣告方式會方法區的字串池中開闢一塊空間存放s1的值,而new的方式建立物件會先在字串常量池檢視是否存在該物件,如果沒有就在字串常量池建立,如果有了就不再建立,因為是通過new建立的物件,所以會在堆區拿出一塊空間存放該物件的值。(具體檢視jvm的結構圖)

string str = "hello"; //如果加乙個空格就加乙個字元 

system.out.println(str.length());

執行結果: 5

string str = ""; //如果str為" "(乙個空格),字串就不為空

if(str.isempty())

else

執行結果: 字串為空

語法 :字串名.charat(值);返回值為char型別。從字串中取出指定位置的字元

string str="劉德華"; 

char c = str.charat(2);

system.out.println("指定字元為:" + c);

執行結果:指定字元為:華

語法:字串名.tochararray();返回值為char陣列型別。將字串變成乙個字元陣列 

string str="劉德華";

char c = str.tochararray(); 

for (int i = 0; i < c.length; i++)

system.out.println("轉為陣列輸出:" + c[i]);

執行結果:

轉為陣列輸出:劉

轉為陣列輸出:德

轉為陣列輸出:華

語法 :字串名.indexof("字元");字串名.indexof("字元",值);查詢乙個指定的字串是否存在,返回的是字串的位置,如果不存在,則返回-1

string str="只為挨你近一點"; 

int a1 = str.indexof("你");// 查詢字元你的位置 

int a2 = str.indexof("為", 2);// 查詢為的位置,從第3個開始查詢 int a3 = str.lastindexof("點"); 

system.out.println("你的位置為:" + a1); 

system.out.println("為的位置為:" + a2);

system.out.println("點最後出現的位置為:" + a3);

執行結果: 你的位置為:3 為的位置為:-1 點最後出現的位置為:7

string str="hello world"; 

system.out.println("將字串轉大寫為:" + str.touppercase());

system.out.println("將字串轉換成小寫為:" + str.touppercase().tolowercase());

運算結果:

將字串轉大寫為:hello world

將字串轉換成小寫為:hello world

string str = "boo:and:foo"; 

string arr1 = str.split(":");

string arr2 = str.split("o");

執行結果:   

arr1  //   

arr2  //

語法 :字串變數名.equals(字串變數名);返回值為布林型別。比較兩個字串是否相等,返回布林值

string str = "hello"; 

string str1="world";

if(str.equals(str1))

else  

執行結果:這倆字串值不相等

string str = "hello world"; 

string str1 = "hello world";

if(str.equalsignorecase(str1))

else

執行結果:str和str1相等

string str = "123我的部落格456"; 

system.out.println("擷取到的字元:" + str.substring(0,3));// 擷取[0,3)的位置 system.out.println("擷取到的字元:" + str.substring(2));// 從第3個位置開始擷取,含2   

執行結果:

擷取到的字元:123

擷取到的字元:3我的部落格456

string str = "hello world"; 

string str1 = "wo"; //區分大小寫

if(str.contains(str1))

else

執行結果:   

str中有wo

string str = "hello world"; 

string str1 = "he";

if(str.startswith(str1))

else

執行結果:

str是he為字首開頭

string str = "我的部落格"; 

string str1 = "部落格";

if(str.endswith(str1))

else

執行結果:

str是以\'部落格\'字尾結束

string s="hello,world".replace('l','w'); 

string s1="hello,world".replacefirst("l", "w");

system.out.println(s);

system.out.println(s1);

執行結果:

hewwo,worwd

hewlo,world //只替換了第乙個l

可能內容比較雜,把自己的薄弱點記錄下來~

常用API 字串

字串的比較 1 通過 字元 陣列來建立乙個字串 2 通過 位元組 陣列來構建乙個字串 3 通過new的方式建立乙個字串 4 通過字面常量來建立乙個字串,位於字串常量池 如是通過字面常量來進行拼接,得到的字元長位於常量池 在拼接過程中只要有乙個是變數則都位於堆區。intern 該方法的作用 1 如果呼...

字串常用API

gets char p puts char p char strcpy char dest,const char stc char strncpy char dest,const char stc,int n char strcat char dest,const char stc 將stc的內容拼...

字串常用的API

獲取字串的長度 str.length 長度裡面包括字串之間的空格以及標點符號 let str hellow world console.log str.length 列印結果為12 獲取字串給定索引位置的字元 chartat let str hellow world console.log str....