A function which replaces an unicode character in a string:
void replaceAllOccurences(std::string& source,
const std::string& replaceFrom,
const std::string& replaceTo)
{
std::vector<std::uint8_t> data(source.begin(), source.end());
std::vector<std::uint8_t> pattern(replaceFrom.begin(), replaceFrom.end());
std::vector<std::uint8_t> replaceData(replaceTo.begin(), replaceTo.end());
std::vector<std::uint8_t>::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 "€".