insert:
erase:
常见用法:
int main()
{
string s1("hello world");
string s2("gm");
s1.insert(5,"x");
cout << s1 << endl;
s1.insert(6,s1,0);
cout << s1 << endl;
s1.insert(0,"x"); //
cout << s1 << endl;
s1.insert(0,1, 'x');
cout << s1 << endl;
s1.erase(5,4);
cout << s1 << endl;
s1.erase(5);
cout << s1 << endl;
return 0;
}
find :
rfind:
replace:
配合使用:
int main()
{
string s1("hello world hello world");
size_t pos = s1.find(' ',0);
while (pos != string::npos)
{
s1.replace(pos,1,"%20");
pos = s1.find(' ',pos+3);
}
cout << s1 << endl;
string s2("hello world hello world");
size_t pos1 = s2.find(' ', 0);
while (pos1 != string::npos)
{
s2.replace(pos1, 2, "%20");
pos1 = s2.find(' ', pos + 3);
}
cout << s2 << endl;
return 0;
}
c_str:
C++中打开文件:
int main()
{
string filename("test.cpp");
FILE* fout = fopen(filename.c_str(), "r");
char ch = fgetc(fout);
while (ch != EOF)
{
cout << ch;
ch = fgetc(fout);
}
cout << endl;
return 0;
}
substr:
找到一个子字符串返回。