0
点赞
收藏
分享

微信扫一扫

C++ 面试题:字符串替换

WikongGuan 2022-02-21 阅读 73
/*
	功  能:替换指定字符
	参  数:str:字符串
			oldValue:替换前字符
			newValue:替换后字符
	返回值:替换指定字符后的字符串
*/

string& ReplaceAll(string &str, const string& oldValue, const string& newValue)
{
	string::size_type pos(0);
	while ((pos = str.find(oldValue,pos)) != string::npos)   //未找到返回npos,不等于就是找到
	{
		str.replace(pos,oldValue.length(),newValue);
		if (newValue.length()>0)
		{
			pos += newValue.length();
		}

	}
	return str;
}
举报

相关推荐

0 条评论