String常用方法

2021-09-25 20:54:11 字數 2656 閱讀 4342

返回型別

方法int

indexof(string str)

返回指定子字串在字串第一次出現的索引。

intindexof(int ch, int fromindex)

返回指定字元第一次出現在字串內的索引,以指定的索引搜尋。

intlastindexof(int ch)

返回指定字元的最後一次出現在字串中的索引。

intlastindexof(int ch, int fromindex)

返回指定字元最後一次出現在字串中的索引,從指定的索引開始向後搜尋。

intlastindexof(string str)

返回指定子字串最後一次出現的字串中的索引

intlastindexof(string str, int fromindex)

返回指定子字串的最後一次出現的字串中的索引,從指定索引開始向後搜尋。

string

replace(char oldchar, char newchar)

將原來的字元替換為新字元

char

tochararray()

將此字串轉換為新的字元陣列。

boolean

contains(charsequence s)

當且僅當此字串包含指定的char值序列時才返回true。

string

concat(string str)

將指定的字串連線到該字串的末尾。

string

charat(int  index)

返回指定索引處的char值。

boolean

equalsignorecase(string anotherstring)

將字串與指定的物件比較,不考慮大小寫。       

intcompareto(string str)

比較的是兩個字串的字典順序,如果兩個字串的字典順序相等,則返回0.不相等則返回差值。

string

tolowercase()

將該字串轉小寫字母

string

touppercase()

將該字串轉大寫字母

equals(string str)

string 的equals包含了「==」,雙等號比較的是位址,儲存位址相同,內容肯定是相同的。

然後當位址不相同的時候,先驗證了比較物件是否是string,接著比較了兩個字串的長度是否相同,最後迴圈比較了字串的每個字元是否相等,完全相同返回true,否則 false

public boolean equals(string str)

if(this==str)

int n=this.value.length;

if(n==str.value.length)

if(prefix==this)

char c1=value;

char c2=prefix.value;

int k=0;

while(k語法2 public boolean startswith(string prefix , int toffset)

返回值:如果引數表示的字串行是此物件從索引toffset處開始的子字串,則返回true;否則返回false。如果toffset為負或大於此string物件的長度,則結果為false;否則結果與該表示式的結果相同。

引數:prefix為指定的字首。

引數:toffset為在字串中開始查詢的位置。

public boolean startswith(string prefix, int toffset)

if(s==this)

for(int i=0;isubstring(int beginindex, int endindex)

擷取指定字串

1、取得的字串長度為:endindex - beginindex;

2、從beginindex開始取,到endindex結束,從0開始數,其中不包括endindex位置的字元

public string substring(int beginindex, int endindex)

if(beginindex<0||endindex>value.length)

char c=new char[endindex-beginindex];

int index=0;

for(int i=beginindex;iconcat(string str)

將指定的字串連線到該字串的末尾。

public string concat(string str)

char c=new char[length()+str.length()];

for(int i=0;ireplace(char oldchar, char newchar)

將原來的字元替換為新字元

public string replace(char oldchar, char newchar)

}return new string(c);

}

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