隨本小記 string常用方法

2021-08-19 05:53:50 字數 1620 閱讀 4079

string類方法小結

string的常用方法:

1、獲取方法

int length();  獲取字串的長度

char charat( int index )   根據指定的下標獲取對應的字元資料

int indexof( int ch )  獲取指定的字元在字串中從前往後第一次出現的下標

int indexof( int ch , int fromindex )  獲取指定的字元在字串中從指定的位置往後第一次出現的下標

int indexof( string str  )  獲取指定的字串在字串中從前往後第一次出現的下標

int indexof( string str ,int fromindex )  獲取指定的字串在字串中從指定的位置往後第一次出現的下標

int lastindexof( int ch )  獲取指定的字元在字串中從後往前第一次出現的下標

int lastindexof( int ch , int fromindex )  獲取指定的字元在字串中從指定的位置往前第一次出現的下標

int lastindexof( string str  )  獲取指定的字串在字串中從後往前第一次出現的下標

int lastindexof( string str ,int fromindex )  獲取指定的字串在字串中從指定的位置往前第一次出現的下標

string substring( int beginindex );擷取字串的子串,從指定下標開始直到字串結束;

string substring( int beginindex  ,int  endindex );;擷取字串的子串,從指定下標(包括)開始直到指定下標(不包括)結束;

2、判斷和比較

boolean  isempty()  判斷字串是否為空

boolean  equals(string  anstring ) 判斷兩個字串是否相等

boolean  contains( string s ) 判斷指定的字串在源串中是否存在

boolean  startswith( string str ) 判斷是否以指定的字串開始

boolean  endswith(string str) 判斷是否以指定的字串結尾

3、轉換

valueof() 把其他的資料轉成字串

tochararray() 字串轉字元陣列

getbytes() 字串轉位元組陣列

string split( string str ) 按照指定的方式切割字串為字串陣列

touppercase()  字串中所有字母都轉大寫

tolowercase()  字串中所有字母都轉小寫

4、其他方法

trim(): 去除兩端的空格

concat(string str):拼接字串;

replace(char?oldchar, char?newchar):用新字元替換字串中所有老字元

replace(charsequence?target, charsequence?replacement):用新字串替換字串中所有老字串

int compareto(string anstring ) 根據兩個字串中的字元在編碼表中的位置返回的是 零  正數  負數

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...