總結一些string函式:
string a="qwe";
string b="asd";
a.insert(2,b);
cout《輸出:qwasde
string a="qwe";
a.erase(0,1);//刪除第乙個字元
cout《結果:we
string a="qwe";
a.replace(2,3,"dfg");//將a的第2個位置後的三個字元替換為dfg,沒有的補上
cout《輸出:qwdfg
string a="qwereet";
int b = a.find('e');//字元也可以,返回找到第乙個字元的位置,若找不到,返回-1,也就是string::npos
int c = a.find("re");
cout《輸出:2 3
rfind():到著找
string a="qwwreet";
int b = a.rfind('e');
int c = a.rfind("re");//第乙個r的位置在倒數第三個
cout《輸出:5 3
//將字串中所有的母音字母換成*
#include#includeusing namespace std;
int main()
std::cout << str << '\n';
return 0;
}//執行結果:
//pl**s* r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks
find_last_of 倒著找
感覺和前面一類的相反的,類似於找了個補集。即在a中搜尋b中沒有的字元並返回位置
#includeusing namespace std;
int main()
cout << str;
return 0;
}輸出:**ea*e***e**a*e***e**o*e***i****i***e**e**e****a**e*i****
#includeusing namespace std;
int main()
輸出:123
C string小貼士總結
標頭檔案 include 從c 11開始引入以下函式 標頭檔案 include 下面的pos就是size t型別的位置。p是size t型別的,其值會被修改數值後第乙個字元所在位置 第乙個字元為0 亦即數值部分的結束位置。外鏈轉存失敗,源站可能有防盜煉機制,建議將儲存下來直接上傳 img 6r09x...
C String建構函式
string 類的原型如下 class string string string string string const char str else 當初始化串存在的時候,為m data申請同樣大小的空間存放該串 string string const string other 拷貝建構函式,功能與...
C string常用函式
string擷取 替換 查詢子串函式 1.擷取子串 s.substr pos,n 擷取s中從pos開始 包括0 的n個字元的子串,並返回 s.substr pos 擷取s中從從pos開始 包括0 到末尾的所有字元的子串,並返回 2.替換子串 s.replace pos,n,s1 用s1替換s中從po...