替换字符串中的指定子串

//input--原字符串

//find--被替换的子串

//replaceWith--替换字符串

 

string stringReplace(const string& input, const string &find, const string &replaceWith)
{
 string strOut(input);
 int curPos = 0;

 int pos;
 while ((pos = strOut.find(find, curPos)) != -1)
 {
  strOut.replace(pos, find.size(), replaceWith);
  curPos = pos + replaceWith.size();
 }
 return strOut;
}

 

在网上搜索的时候看到好多人都使用了replace()/replaceAll()函数,但是我却不能用。不知道这些函数到底是标准库原来就有的,还是自己写的。

另外就是这里如果用char很不好用的。

比如说函数定义为

char* stringReplace(const int len)

{

     char *strOut = new char[len];

     return strOut;

}

//这是绝对错误的。但是有什么好的方法去避免吗?………………

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值