I have created a function that replaces a string.
It looks like this:
void replace_with(wstring& src, const wstring& what, const wstring& with)
{
if (what != with) {
wstring temp;
wstring::size_type prev_pos = 0, pos = src.find(what, 0);
while ( wstring::npos != pos ) {
temp += wstring(src.begin() + prev_pos, src.begin() + pos) + with;
prev_pos = pos + what.size();
pos = src.find(what, prev_pos);
}
if ( !temp.empty() ) {
src = temp + wstring(src.begin() + prev_pos, src.end());
if (wstring::npos == with.find(what)) {
replace_with(src, what, with);
}
}
}
}
However, if my string is size==1, and the "what" is exactely the string, it will not replace it.
For example
wstring sThis=L"-";
replace_with(sThis,L"-",L"");
... will not replace the "-".
I don't see where I went wrong.
Can anybody help, please?
std::string::replace?