Use of C + + string substitution functions _c language

Source: Internet
Author: User
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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.