String字串的使用

2021-07-14 11:32:05 字數 1277 閱讀 6356

1、字串比較

equals() ——判斷內容是否相同。

compareto() ——判斷字串的大小關係。

comparetoignorecase(string int) ——在比較時忽略字母大小寫。

== ——判斷內容與位址是否相同。

equalsignorecase() ——忽略大小寫的情況下判斷內容是否相同。

reagionmatches() ——對字串中的部分內容是否相同進行比較(詳情請參考api)。

2、字串查詢

charat(int index) ——返回指定索引 index 位置上的字元,索引範圍從 0 開始。

indexof(string str)——從字串開始檢索str,並返回第一次出現的位置,未出現返回 -1。

indexof(string str,int fromindex);——從字串的第 fromindex 個字元開始檢索 str。

lastindexof(string str)——查詢最後一次出現的位置。

lastindexof(string str,int fromindex)—-從字串的第fromindex個字元查詢最後一次出現的位置。

starwith(string prefix,int toffset)—–測試此字串從指定索引開始的子字串是否以指定字首開始。

starwith(string prefix)——測試此字串是否以指定的字首開始。

endswith(string suffix)——測試此字串是否以指定的字尾結束。

3、字串擷取

public string substring(int beginindex)——返回乙個新的字串,它是此字串的乙個子字串。

public string substring(int beginindex,int endindex)——返回的字串是從 beginindex 開始到 endindex-1 的串。

4、字串替換

public string replace(char oldchar,char newchar)。

public string replace(charsequence target,charsequence replacement)——把原來的 etarget 子串行替換為 replacement 序列,返回新串。

public string replaceall(string regex,string replacement)——用正規表示式實現對字串的匹配。注意 replaceall 第乙個引數為正規表示式,鄙人曾經深受其害。

5、更多方法請參考 api

go redis使用之String字串

go redis list列表使用詳解 go redis hash使用詳解 go redis set集合使用詳解 go redis zset有序集合使用詳解 基本型別 字串操作 單個字串最大512m func getredisclient redis.client func redistest cl...

string資源的格式字串使用

在資源檔案中這麼寫 我的名字叫 1 s,今年 2 d歲 1代表第乙個格式字串的位置,從1開始 s 代表字串 d 代表數字 在程式中的使用 textview tv textview findviewbyid r.id.textview string str getresources getstring...

字串排序 string類的使用

最近幫他們做了乙個簡單的c 的題目,以前做過,當時是借鑑的別人的 現在也忘得差不多了,不過思路還有,現在正好可以再溫習一下。題目要求如下 先輸入你要輸入的字串的個數。然後換行輸入該組字串。每個字串以回車結束,每個字串不多於一百個字元。如果在輸入過程中輸入的乙個字串為 stop 也結束輸入。然後將這輸...