C++学习之string类常用函数整理

一,string.substr()函数

    1.重载原型为string substr(_off=0,_count=npos) ; 

    npos一般表示为string类中不存在的位置,_off表示字符串的开始位置,_count截取的字符的数目

  1. #include<string>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     string x="Hello_World";  
  6.     cout<<x.substr()<<endl;  //默认截取全部字符串
  7.     cout<<x.substr(5)<<endl;//截取x[5]到结尾,即npos.重载原型为string substr(_off,_count=npos)  
  8.     cout<<x.substr(0,5)<<endl;//以x[0]为始,向后截取5位(包含x[0]),重载原型string substr(_off,_count)  
  9.     /* 
  10.     备注: 
  11.     指定的截取长度加起始位置即_off+_count>源字符串的长度,则子字符串将延续到源字符串的结尾 
  12.     */  
  13. }  

二,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"都可以

  1. #include <iostream>  
  2. #include <string.h>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.    string s="hahahaha";  
  8.    string s1="a";  
  9.    int position;  
  10.    position=s.find_first_of(s1);  
  11.    cout<<position<<endl;  
  12.    return 0;  
  13. }  //输出:1

3.从字符串s下标为a开始查找字符串s1,返回起始位置  s.find(s1,a); 查找不到返回-1

  1. #include <iostream>  
  2. #include <string.h>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.    string s="have a good time";  
  8.    string s1="good";  
  9.    int position;  
  10.    position=s.find(s1,3);  
  11.    cout<<position<<endl;  
  12.    position=s.find(s1,12);  
  13.    cout<<position<<endl;  
  14.    return 0;  
  15. }
  16. //输出:
  17. //7
  18. //-1

4.查找字符s1在s中出现的所有起始位置

  1. #include <iostream>  
  2. #include <string.h>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.    string s="abb abb abb";  
  8.    string s1="a"//char s1='a';  
  9.    int position=0;  
  10.    int i=0;  
  11.    while((position=s.find_first_of(s1,position))!=string::npos)  
  12.    {  
  13.        cout<<position<<" ";  
  14.        position++;  
  15.        i++;//i为出现的总次数  
  16.    }  
  17.    cout<<endl<<"total="<<i<<endl;  
  18.    return 0;  
  19. } //输出:
  20. //0  4  8
  21. //total=3

5.查找字符串s1与s第一个不匹配的位置

  1. #include <iostream>  
  2. #include <string.h>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.    string s="aaabcd";  
  8.    string s1="aaafcd";  
  9.    int position;  
  10.    position=s.find_first_not_of(s1);  
  11.    cout<<position<<endl;  
  12.    return 0;  
  13. }  //输出:3

6.反向查找,返回s1在s中的位置

  1. #include <iostream>  
  2. #include <string.h>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.    string s="aaabcd";  
  8.    string s1="abc";  
  9.    int position;  
  10.    position=s.rfind(s1);  
  11.    cout<<position<<endl;  
  12.    return 0;  
  13. }  //输出:2

三,string.erase()函数

1、string.erase(pos,n)          //删除从pos开始的n个字符    string.erase(0,1);   删除第一个字符

  1. #include <string>  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.    string::iterator i;  
  9.    string s;  
  10.    cin>>s;  
  11.    s.erase(1,2);  
  12.    cout<<s;  
  13.     return 0;  
  14. }  

2、string.erase(pos)           //删除pos处的一个字符(pos是string类型的迭代器)

  1. #include <string>  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.    string::iterator i;  
  9.    string s;  
  10.    cin>>s;  
  11.    i = s.begin()+3;  
  12.    s.erase(i);  
  13.    cout<<s;  
  14.     return 0;  
  15. }  


3、string.erase(first,last)    //删除从first到last中间的字符(first和last都是string类型的迭代器)


  1. #include <string>  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.    string::iterator i;  
  9.    string s;  
  10.    cin>>s;  
  11.    s.erase(s.begin()+1,s.end()-1);  
  12.    cout<<s;  
  13.     return 0;  
  14. }  

四,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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值