Replace unicode character in the string A function which replaces an unicode character in a string:

void replaceAllOccurences(std::string&amp; source,
                          const std::string&amp; replaceFrom,
                          const std::string&amp; replaceTo)
{
    std::vector&lt;std::uint8_t&gt; data(source.begin(), source.end());
    std::vector&lt;std::uint8_t&gt; pattern(replaceFrom.begin(), replaceFrom.end());
    std::vector&lt;std::uint8_t&gt; replaceData(replaceTo.begin(), replaceTo.end());

    std::vector&lt;std::uint8_t&gt;::iterator itr;
    while((itr = std::search(data.begin(), data.end(), pattern.begin(), pattern.end())) != data.end())
    {
        data.erase(itr, itr + pattern.size());
        data.insert(itr, replaceData.begin(), replaceData.end());
    }

    source = std::string(data.begin(), data.end());
}


Usage:

std::string source = "123€AAA€BBB";
std::string replaceFrom = "€";
std::string replaceTo = "\x80";

replaceAllOccurences(source, replaceFrom, replaceTo);


replaceTo may be get from some external conversion library, for example: iconvpp. Normally I would convert the whole source using iconvpp library but I have a case where I need to convert only particular character, for example "€".
If you're not worried about performance, you can replace all the manual work by using the &lt;regex&gt; facilities, which results in a considerable reduction of code to test and maintain.

#include &lt;regex&gt;

source = std::regex_replace(source, std::regex("€"), "\x80");


I would still keep it in a separate function to make it easy to change the implementation afterwards.