In C ++, there are many methods for string replacement. Here we mainly talk about replacement in WString in STL. Although WString comes with a Replace function, it can only be replaced once, which is too bad, therefore, a replacement function is written separately.
[Function]
Copy codeThe Code is as follows :/**
* @ Brief string replacement
* @ Param orignStr source string
* @ Param oldStr
* @ Param newStr replaces the New String
* @ Return returns the modified string
*/
Static wstring Replace (const wstring & orignStr, const wstring & oldStr, const wstring & newStr );
[Implementation]Copy codeThe Code is 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;
}