c++ string的查找与替换

发布时间:2022-06-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了c++ string的查找与替换脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

功能描述:查找:查找指定字符串是否存在替换:在指定的位置替换字符串函数原型:int find(const string& str, int pos = 0) const; //查找str第一次出现位置,pos开始查找int find(const char* s, int pos = 0) const; //查找s第一次出现位置,pos开始查找int find(const char* s, int pos, int n) const; //pos位置查找s的前n个字符第一次位置int find(const char c, int pos = 0) const; //查找字符c第一次出现位置int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,pos开始查找int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,pos开始查找int rfind(const char* s, int pos, int n) const; //pos查找s的前n个字符最后一次位置int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串strstring& replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串s

注意,rfind是从左往右找,同时pos位置指的是从左往右忽略到第几个位置,但是最终得到的结果是正常从右往左的序列结果,比如rfind(“a”,0)最后得到的结果一定是0或者-1,因为其他位置的都被忽略掉了。另一方面,注意replace的时候,替换的数量是输入的,但是实际插入的结果受实际输入制约,比如替换3个,但是输入了四个,结果也是替换掉了三个,但是插入了四个

 

  string s1 = "abcdefgaaa";
  int pos = s1.find("d1",0);
  cout << pos <<endl;
  int poe = s1.rfind("de",7);
  cout << poe <<endl;
  s1.replace(1,3,"11111");
  cout << s1 <<endl;
  s1.replace(6,3,"1");
  cout << s1 <<endl;
 
-13a11111efgaaaa111111aaa

脚本宝典总结

以上是脚本宝典为你收集整理的c++ string的查找与替换全部内容,希望文章能够帮你解决c++ string的查找与替换所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。