In C + +, there are many methods of string substitution, this is mainly about the wstring in the STL in the replacement, although wstring with a replace function, but only once, too bad, so write a separate replacement function
function
Copy Code code as follows:
/**
* @brief Implement string substitution
* @param orignstr Source string
* @param oldstr to find the string
* @param a new string of NEWSTR replacements
* @return return the modified string
*/
Static wstring Replace (const wstring& ORIGNSTR, const wstring& OLDSTR, const wstring& NEWSTR);
Achieve
Copy Code code as follows:
Std::wstring Replace (const wstring& ORIGNSTR, const wstring& OLDSTR, const wstring& NEWSTR)
{
size_t pos = 0;
Wstring tempstr = orignstr;
Wstring::size_type Newstrlen = Newstr.length ();
Wstring::size_type Oldstrlen = Oldstr.length ();
while (true)
{
pos = Tempstr.find (Oldstr, POS);
if (pos = = Wstring::npos) break;
Tempstr.replace (POS, Oldstrlen, NEWSTR);
POS + Newstrlen;
}
return tempstr;
}