C++ 字符串替换

/******************************************************************************************
Function:        replace_all
Description:     字符串全部替换
Input:           str:源字符串 old_value:查找的字符串 new_value: 替换的新字符串
Return:          替换后的字符串
*******************************************************************************************/
std::string& replace_all(std::string& str, const std::string& old_value, const std::string& new_value)
{
	try
	{
		for (std::string::size_type pos(0); pos != std::string::npos; pos += new_value.length())
		{
			if ((pos = str.find(old_value, pos)) != std::string::npos)
			{
				str.replace(pos, old_value.length(), new_value);
			}
			else {
				break;
			}
		}
	}
	catch (std::exception e)
	{
	}

	return  str;
}

猜你喜欢

转载自blog.csdn.net/sylsjane/article/details/80867994