String類的轉換功能

2022-09-23 12:12:07 字數 1088 閱讀 9614

//定義乙個字串

string s1 = "abc";

//byte getbytes(); 將字串轉換成位元組陣列

byte bys = s1.getbytes(); //97,98,99

for (int i = 0 ; i字串「123」 此為靜態方法,可以直接呼叫

string s2 = string.valueof(123);

system.out.println(s2+4); //1234

//在實際開發中,上述的方式基本上都會用下邊的這行**替代

string s3 = "" + 123 ;

system.out.println(s3 + 4);

system.out.println("********************=");

//string replace(old,new); 將指定字元(串)替換成新的字元(串)

string s4 = "acc acc acc";

//'d' 替換'c'

string s5 = s4.replace('c','d');

system.out.println("s5:"+s5);

system.out.println("********************=");

//string split(string);切割字串,返回切割後的字串資料,原字串不變

//將字串s4,按照空格進行切割

String的轉換功能

package cn.itcast 05 string的轉換功能 byte getbytes 把字串轉換為位元組陣列。char tochararray 把字串轉換為字元陣列。static string valueof char chs 把字元陣列轉成字串。static string valueof ...

String類的獲取功能

string類的獲取功能 int length 獲取字串的長度 char charat int index 返回指定索引處的字元 重點 int indexof int ch 返回回指定字元在此字串中第一次出現處的索引 重點 這裡的引數是int型別,而不是char型別 a 和97 都代表 a int ...

String類的獲取功能

int length 獲取字串的長度。char charat int index 獲取指定索引位置的字元 int indexof int ch 返回指定字元在此字串中第一次出現處的索引。int indexof string str 返回指定字串在此字串中第一次出現處的索引。int indexof i...