现在的位置: 首页 > 综合 > 正文

std::string实现全部替换功能

2013年02月03日 ⁄ 综合 ⁄ 共 341字 ⁄ 字号 评论关闭
想把"/n"换成"//n"怎么办?因为std::string并未实现替换所有子串功能,故封装一个函数
void StringReplace(string &strBase, string strSrc, string strDes)
{
	string::size_type pos = 0;
	string::size_type srcLen = strSrc.size();
	string::size_type desLen = strDes.size();
	pos=strBase.find(strSrc, pos); 
	while ((pos != string::npos))
	{
		strBase.replace(pos, srcLen, strDes);
		pos=strBase.find(strSrc, (pos+desLen));
	}
}

 

抱歉!评论已关闭.