zoukankan      html  css  js  c++  java
  • string类自定义字符串替换函数replace

    #include <iostream>

    #include <string>

    using namespace std;

    /*

    *  函数功能:将string字符串中的某些字符替换成其他字符

    *  参数说明:  str 原字符串  strFind 需要替换字符串   strReplace 替换字符串

    */

    string replace(string &str,const string &strFind,const string &strReplace)

    {    

      int fsize = strFind.size();  

      int rsize = strReplace.size();

      int pos = str.find(strFind,0);//从开始位置查找第一个符合的字符串,未找到返回str.npos

       while (pos!=str.npos)

       {  

         str.replace(pos,rsize,strReplace); //替换查找到的字符串

         pos = str.find(strFind,pos+fsize); //继续查找

       }

       return str;

    }

    int main(int argc, char* argv[])

    {

       string str="abc[def[gef[ee[";  

       string strFind ="[";  

       string strReplace="(";

       replace(str,strFind,strReplace);  //将字符串str中的“[”替换为“(”

      cout<<str<<endl;  

      return 0;

    }

  • 相关阅读:
    react
    问题总结21-07-12至21-08-15
    排序
    问题总结21-06-28至21-07-11
    问题总结21-06-14至21-06-27
    问题总结21-05-29至21-06-13
    问题总结21-04-19至21-05-28
    问题总结21-03-29至21-04-18
    问题总结21-03-08至21-03-28
    问题总结21-03-01至21-03-07
  • 原文地址:https://www.cnblogs.com/marshhu/p/3434453.html
Copyright © 2011-2022 走看看