string容器的各个方法
#include <bits/stdc++.h> using namespace std; void find_pos(){ string a="abcd123bcdaa"; cout <<a.find("bcd")<<endl;//find返回字符串第一个字符的位置 :从头找查找第一次出现的位置 cout <<a.rfind("bcd");//从后面开始查找,找到最后一次出现的位置 } void replace(){ string s1="bahas1234"; s1.replace(0,3,"111"); cout <<s1; } void compare1(){ string s1="abcd",s2="bcd"; if(s1.compare(s2)==0) cout<<"字符串相等"; else cout<<"字符串不相等"; } void Get(){//获取子串 string s1="abcd"; cout<<s1.substr(1,2); } void insert_del(){ string s1="abcdefg"; s1.insert(4,"111"); cout <<s1<<endl; s1.erase(2,4);//参数为删除区间 cout <<s1<<endl; } int main(){ // find_pos(); // replace(); // compare1(); // Get(); insert_del(); }