C 字串處理

2021-06-27 01:38:56 字數 1261 閱讀 1163

常見字串處理方法

1.字串比較

字串.comparto(目標字串)

"a".comparto("b");

2.查詢子串

字串.indexof(子串,查詢其實位置) ;

字串.lastindexof(子串) ;最後一次出現的位置

str.indexof("ab",0);

3.插入子串

字串.insert(插入位置,插入子串) ;

s.insert(2,"ab");

4.移出子串

字串.remove(其實位置,移出數);

s.remove(3,2);

5.替換子串

字串.replace(源子串,替換為);

s.replace("-"," ");將-替換為空格

6.擷取子串

字串.substring(擷取其實位置,擷取個數);

"abcdef".substring(2,3);結果為cde

7.去空格

mystring = mystring.trim(); //同時刪除字串前後的空格

char trimchars = ; //準備刪除的字元

mystring = mystring.trim(trimchars); //刪除所有指定字元

mystring = mystring.trimend(); //刪除字串後的空格

mystring = mystring.trimstart(); //刪除字串前的空格

8.轉換大小寫

str.tolower()轉化成小寫字母

str.toupper()轉化成大寫字母

char.isupper(str,3)//判斷乙個字串中的第n個字元是否是大寫

9.拆分字串

string strs = mystring.split(' ',3); //按照空格進行拆分,並且返回前三個字串,結果在字串陣列裡

10.使字串達到指定長度

padleft()、padright() ,如:

string str1="00";

str1=str1.padleft(3,'1'); //無第二引數為加空格,結果為「100」

11.得到字串長度

len=str.length;

12.字串遍歷

string mystring = "this is a test!";

foreach (char mychar in mystring)

",mychar);

}

C 字串處理

private static regex regnumber new regex 0 9 private static regex regnumbersign new regex 0 9 private static regex regdecimal new regex 0 9 0 9 privat...

C 字串處理

string字串是char的集合,而char是unicode的 所以char可以轉化為int。字串在引數傳遞時為引用傳遞 可以使用空字串 一 字串型別轉換 1.轉為char 可以用索引器來得到字串中指定的字元,如 string mystring hello char mychars mychars ...

C 字串處理

void memccpy void dest,const void src,int c,size t n 從src所指向的物件複製n個字元到dest所指向的物件中。如果複製過程中遇到了字元c則停止複製,返回指標指向dest中字元c的下乙個位置 否則返回null。void memcpy void de...