String 基本字元操作

2021-10-02 18:26:48 字數 3282 閱讀 3660

排序比較

大小寫切換

判斷首尾字元

替換去除首尾空格

example

trap

int

indexof

(int ch)

//返回指定字元在此字串中第一次出現處的索引。

intindexof

(string str)

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

intindexof

(int ch,

int fromindex)

//返回在此字串中第一次出現指定字元處的索引,從指定的索引開始搜尋。

intindexof

(string str,

int fromindex)

//返回指定子字串在此字串中第一次出現處的索引,從指定的索引開始。

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

intlastindexof

(int ch)

//返回指定子字串在此字串中最右邊出現處的索引。

intlastindexof

(string str)

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

intlastindexof

(int ch,

int fromindex)

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

intlastindexof

(string str,

int fromindex)

//返回乙個新的字串,從指定index擷取,包含begin。 

string substring

(int beginindex)

//返回乙個新字串,包頭不包尾。

string substring

(int beginindex,

int endindex)

#按字典順序比較兩個字串。

intcompareto

(string anotherstring)

static comparator

case_insensitive_order

乙個對 string 物件進行排序的 comparator,作用與 comparetoignorecase 相同

public

static

void

main

(string[

] args)

; system.out.

println

(arrays.

tostring

(rawstrings));

// 使用string.case_insensitive_order進行排序

arrays.

sort

(rawstrings, string.case_insensitive_order)

; system.out.

println

(arrays.

tostring

(rawstrings));

}

#將此 string 與另乙個 string 比較,不考慮大小寫。 

boolean

equalsignorecase

(string anotherstring)

string tolowercase() 

使用預設語言環境的規則將此 string 中的所有字元都轉換為小寫。

string tolowercase

(locale locale)

使用給定 locale 的規則將此 string 中的所有字元都轉換為小寫。

string touppercase()

使用預設語言環境的規則將此 string 中的所有字元都轉換為大寫。

string touppercase

(locale locale)

使用給定 locale 的規則將此 string 中的所有字元都轉換為大寫。

boolean

startswith

(string prefix)

測試此字串是否以指定的字首開始。

boolean

startswith

(string prefix,

int toffset)

測試此字串從指定索引開始的子字串是否以指定字首開始。

boolean

endswith

(string suffix)

測試此字串是否以指定的字尾結束。

string replace

(char oldchar,

char newchar)

返回乙個新的字串,它是通過用 newchar 替換此字串**現的`所有` oldchar 得到的。

string replace

(charsequence target, charsequence replacement)

使用指定的序列替換此字串`所有`匹配的子字串。

string replaceall

(string regex, string replacement)

使用給定的 replacement 替換此字串所有匹配給定的正規表示式的子字串。

string replacefirst

(string regex, string replacement)

使用給定的 replacement 替換此字串匹配給定的正規表示式的第乙個子字串。

string trim

()

str4 = str4.

substring(0

, str4.

length()

-1);

suffix = str.

substring

(str.

lastindexof

(".")+

1)

stringbuilder和string進行equals比較時,即使值一摸一樣,也會報不相等,要先轉成tostring()

string基本字元容器

使用string容器,需要標頭檔案 include,下面介紹string的幾種用法 1.建立string物件 string s 2.給string物件賦值 2.1 直接賦值 s abcdfeg 2.2用字元指標賦值 string s char ss 100 scanf s ss s ss 注意 sc...

String基本字元系列容器

1 建立s 空字串 include include include include include include include includeusing namespace std int main string s cout 2 基本字元系列容器給string物件賦值 直接給字串物件賦值 in...

string基本字串行容器

c語言每天提供專門的字串型別,需要通過字元陣列才能對字串進行儲存和處理。在標準c 中,字串類由c stl實現。string是乙個基於字元的序列容器,具有vector向量一樣的內部線性結構,字元逐一寫入容器,最後以null字元結尾。跟傳統的char 字元陣列相比,string提供了豐富的函式用於字元的...