1、string插入替换
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s = "do";
s.insert(0, "how ");
s.append(" you");
s = s + " do?";
cout << s << endl;
string s1 = "what's your name";
s1.replace(7, 4, "her");
cout << "替换后" << s1 << endl;
}
2、字符串整型相互转化
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
int n1 = 10;
string s1;
stringstream os1;
os1 << n1;
os1 >> s1;
cout << "整型10转化为字符串:" << s1 << endl;
int n2 = 0;
string s2 = "123";
stringstream os2;
os2 << s2;
os2 >> n2;
cout << "字符串123转化为整型:" << n2 << endl;
}
3、字符串查询
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s = "my name is xiao de guang";
int n=s.find("xiao",7);
if (n!=string::npos)
cout << n << endl;
n = s.find_first_of("xiao",8);
if (n != string::npos)
cout << n << endl;
n = s.find_first_not_of("xiao", 8);
if (n != string::npos)
cout << n << endl;
n = s.rfind("xiao");
if (n != string::npos)
cout << n << endl;
s.erase();
cout << s;
}