面試6 String常用方法

2021-09-22 01:33:23 字數 954 閱讀 4739

字串獲取:public int length():獲取字串當中含有的字元個數,拿到字串長度。

public string concat(string str):將當前字串和引數字串拼接成為返回值新的字串。

public char charat(int index):獲取指定索引位置的單個字元。(索引從0開始。)

public int indexof(string str):查詢引數字串在本字串當中首次出現的索引位置,如果沒有返回-1值。

字串的擷取方法:public string substring(int index):擷取從引數位置一直到字串末尾,返回新字串。

public string substring(int begin, int end):擷取從begin開始,一直到end結束,中間的字串。

備註:[begin,end),包含左邊,不包含右邊。

public char tochararray():將當前字串拆分成為字元陣列作為返回值。

public byte getbytes():獲得當前字串底層的位元組陣列。

public string replace(charsequence oldstring, charsequence newstring):

將所有出現的老字串替換成為新的字串,返回替換之後的結果新字串。

備註:charsequence意思就是說可以接受字串型別。

分割字串的方法:public string split(string regex):按照引數的規則,將字串切分成為若干部分。

注意事項:

split方法的引數其實是乙個「正規表示式」,今後學習。今天要注意:如果按照英文句點「.」進行切分,必須寫"\\."(兩個反斜槓)

String常用方法

1,startswith判斷是否以某字串開始 2,endswith判斷是否以某字串結尾 3,contains判斷是否包含另乙個字串 4,substring取出指定位置的字串 5,charat找到指定位置的字元 6,indexof正向找到指定字元的位置 7,lastindexof反向找到指定字元的位置...

string 常用方法

例項化方法建立字串 instancetype initwithstring nsstring astring instancetype initwithformat nsstring format,instancetype initwithutf8string const char bytes 類方...

String常用方法

public class teststring string str hello string的方法 1 跟字元陣列有關的方法 物件的長度 char array str.tochararray 把string物件轉換成char陣列 根據下標得到string物件該下標位置的字元 l 得到某個字元在st...