一,string.substr()函数
1.重载原型为string substr(_off=0,_count=npos) ;
npos一般表示为string类中不存在的位置,_off表示字符串的开始位置,_count截取的字符的数目。
- #include<string>
- using namespace std;
- int main()
- {
- string x="Hello_World";
- cout<<x.substr()<<endl; //默认截取全部字符串
- cout<<x.substr(5)<<endl;//截取x[5]到结尾,即npos.重载原型为string substr(_off,_count=npos)
- cout<<x.substr(0,5)<<endl;//以x[0]为始,向后截取5位(包含x[0]),重载原型string substr(_off,_count)
- /*
- 备注:
- 指定的截取长度加起始位置即_off+_count>源字符串的长度,则子字符串将延续到源字符串的结尾
- */
- }
二,string.find()函数
1.返回字符串s1在s中的位置,如果没有找到,则返回-1
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string s="what are you dong";
string s1="are";
int position;
position=s.find(s1);
if(position==-1)
cout<<"not find"<<endl;
else
cout<<"position= "<<position<<endl;
return 0;
} //输出:5
2.返回任意字符s1在s中第一次出现的位置,s1为字符,不能为字符串 'a' "a"都可以
- #include <iostream>
- #include <string.h>
- using namespace std;
- int main()
- {
- string s="hahahaha";
- string s1="a";
- int position;
- position=s.find_first_of(s1);
- cout<<position<<endl;
- return 0;
- } //输出:1
3.从字符串s下标为a开始查找字符串s1,返回起始位置 s.find(s1,a); 查找不到返回-1
- #include <iostream>
- #include <string.h>
- using namespace std;
- int main()
- {
- string s="have a good time";
- string s1="good";
- int position;
- position=s.find(s1,3);
- cout<<position<<endl;
- position=s.find(s1,12);
- cout<<position<<endl;
- return 0;
- }
- //输出:
- //7
- //-1
4.查找字符s1在s中出现的所有起始位置
- #include <iostream>
- #include <string.h>
- using namespace std;
- int main()
- {
- string s="abb abb abb";
- string s1="a"; //char s1='a';
- int position=0;
- int i=0;
- while((position=s.find_first_of(s1,position))!=string::npos)
- {
- cout<<position<<" ";
- position++;
- i++;//i为出现的总次数
- }
- cout<<endl<<"total="<<i<<endl;
- return 0;
- } //输出:
- //0 4 8
- //total=3
5.查找字符串s1与s第一个不匹配的位置
- #include <iostream>
- #include <string.h>
- using namespace std;
- int main()
- {
- string s="aaabcd";
- string s1="aaafcd";
- int position;
- position=s.find_first_not_of(s1);
- cout<<position<<endl;
- return 0;
- } //输出:3
6.反向查找,返回s1在s中的位置
- #include <iostream>
- #include <string.h>
- using namespace std;
- int main()
- {
- string s="aaabcd";
- string s1="abc";
- int position;
- position=s.rfind(s1);
- cout<<position<<endl;
- return 0;
- } //输出:2
三,string.erase()函数
1、string.erase(pos,n) //删除从pos开始的n个字符 string.erase(0,1); 删除第一个字符
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- string::iterator i;
- string s;
- cin>>s;
- s.erase(1,2);
- cout<<s;
- return 0;
- }
2、string.erase(pos) //删除pos处的一个字符(pos是string类型的迭代器)
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- string::iterator i;
- string s;
- cin>>s;
- i = s.begin()+3;
- s.erase(i);
- cout<<s;
- return 0;
- }
3、string.erase(first,last) //删除从first到last中间的字符(first和last都是string类型的迭代器)
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- string::iterator i;
- string s;
- cin>>s;
- s.erase(s.begin()+1,s.end()-1);
- cout<<s;
- return 0;
- }
四,string.insert()
string的成员函数insert有以下多种重载:
string &insert(int p0, const char *s);——在p0位置插入字符串s
string &insert(int p0, const char *s, int n);——在p0位置插入字符串s的前n个字符
string &insert(int p0,const string &s);——在p0位置插入字符串s
string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字符串s从pos开始的连续n个字符
string &insert(int p0, int n, char c);//在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iteratorlast);//在it处插入从first开始至last-1的所有字符
void insert(iterator it, int n, char c);//在it处插入n个字符c