string查找和替换

1.用法描述:

  1. 查找:查找指定字符串是否存在
  2. 替换:在指定的位置替换字符串

2.函数原型:

  1. int find(const string &str,int pos=0) const; //从pos位置开始查找str第一次出现的位置
  2. int find(const char *s,int pos=0) const;//从pos位置开始查找s第一次出现位置
  3. int find(const char *s,int pos=0,int n) const; //从pos位置查找s的前n个字符第一次位置
  4. int find(const char c,int pos=0)  const; //从pos位置查找字符c的第一次出现位置
  5. int rfind(const string &str,int pos=npos) const; //从pos开始查找str最后一次位置,
  6. int rfind(const char *s,int pos=npos) const; //从pos开始查找s最后一次出现位置,
  7. int rfind(const char *s,int pos,int n) const; //从pos查找s的前n个字符最后一次位置
  8. int rfind(const char c,int pos=0) const;//查找字符c最后一次出现位置
  9. string &replace(int pos,int n,const string &str);//替换从pos开始的n个字符为str
  10. string &replace(int pos,int n,const char *s);//替换从pos开始的n个字符为字符串s
  11. rfind和find的区别:find默认从左往右查找,而rfind则代表从右往左查;find找到字符串后返回查找到的字符串的第一个字符的下标,找不到则返回-1;replace替换时要指定从哪儿个位置开始起,其后有多少个字符,替换成什么样的字符串。

3.补充:

  1. find_first_of()表示查找字符串的某个字符最先出现的位置,find_first_of()不是全匹配,即它不是必须要查找的字符串在被查找的字符串中全部出现,而是出现个别字符即可。该函数是将被查找的字符串和查找的字符串中第一个重复的字符的下标返回回来。
  2. find_last_of()函数与find_first_of()功能差不多,只不过find_first_of()是从字符串的前面往后面搜索,而find_last_of()是从字符串的后面往前面搜索。
  3. find_first_not_of()函数是找到第一个不与子串匹配的位置。
    #include<iostream>
    #include<string>
    using namespace std;
    
    void search_factor()
    {
    	int pos;//pos的全称为position,即位置的意思。 
    	
    	//1.查找字符串“wo”在字符串"hello world!hello world!"中第一次出现的位置 
    	string s1="hello world!hello world!";
     	pos=s1.find("wo") ;
     	if(pos==-1)
     		cout<<"查找失败!"<<endl;
    	 else
    		cout<<"找到字符位置,其位置pos = "<<pos<<endl;
    		
    	//2. 查找字符串“wo”在字符串"hello world!hello world!"中最后一次出现的位置
    	pos=s1.rfind("wo");
    	cout<<"找到字符位置,其位置pos = "<<pos<<endl;
    	
    	//3.查找字符串hello在字符串dasudgfhshelldasdhello中第一次出现的位置
    	string s2="dasudgfhshelldasdhello";
    	string s3="hello";
    	pos=s2.find(s3,0);
    	cout<<"字符串hello在字符串dasudgfhshelldasdhello中第一次出现的位置为:"<<pos<<endl;
    	
    	//4.从前往后查,查找字符串"hello"中首个与字符串"dasudgfhshelldasdhello"重复的字符位置
    	   //具体起来就是先查h是否存在于母串中,如果没有就查e是否在母串中 ,依次往下查,查到即返回下标 
    	pos=s2.find_first_of(s3);
    	cout<<"从前往后查,字符串hello中首个与字符串dasudgfhshelldasdhello重复的字符位置是:"<<pos<<endl;
    	
    	//5.从后往前查,查找字符串"hello"中首个与字符串"dasudgfhshelldasdhello"重复的字符位置
    	pos=s2.find_last_of(s3) ;
    	cout<<"从后往前查,字符串hello中首个与字符串dasudgfhshelldasdhello重复的字符位置是:"<<pos<<endl;
    	
    	//6.查找字符串"hello"中首个与字符串"hhhhehhllloo"不重复的字符位置
    	string str="hhhhewhhllloo";
    	pos=str.find_first_not_of(s3);
    	cout<<"查找字符串hello中首个与字符串hhhhehhllloo不重复的字符位置: "<<pos<<endl; //返回e的下标 
    	
    	//7.查找字符串"hello"在字符串"hhhhehellohlllohelloo"中出现的所有位置
    	string ss="hhhhehellohlllohelloo";
    	string s="hello";
    	pos=0;  
    	int i=1;  
    	cout<<"查找字符串hello在字符串hhhhehellohlllohelloo中出现的所有位置: "<<endl;
    	while((pos=ss.find(s,pos))!=string::npos)  
    	{  
    	  cout<<"第"<<i<<"个位置: "<<pos<<endl;  
    	  pos++;  
    	  i++;  
     	}  
    	
    }
    void replace_factor()
    {
    	string s2="hello world!hello world!";
    	s2.replace(1,3,"aaaa"); //从下标1开始去除3个字符 ,在下标位置1处插入新字符串 
    	cout<<"s2 = "<<s2<<endl;
    }
    int main()
    {
    	search_factor();
    	replace_factor();
    	return 0;
     } 
    
    /*
    打印结果:
    找到字符位置,其位置pos = 6
    找到字符位置,其位置pos = 19
    字符串hello在字符串dasudgfhshelldasdhello中第一次出现的位置为:17
    从前往后查,字符串hello中首个与字符串dasudgfhshelldasdhello重复的字符位置是:7
    从后往前查,字符串hello中首个与字符串dasudgfhshelldasdhello重复的字符位置是:21
    查找字符串hello中首个与字符串hhhhehhllloo不重复的字符位置: 5
    查找字符串hello在字符串hhhhehellohlllohelloo中出现的所有位置:
    第1个位置: 5
    第2个位置: 15
    s2 = haaaao world!hello world!
    */

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值