I want to replace a string within a string with *
, using this code to replace everything between he
and ld
in helloworld
:
#include <string>
#include <iostream>
int main()
{
const std::string msg = "helloworld";
const std::string from = "he";
const std::string to = "ld";
std::string s = msg;
std::size_t startpos = s.find(from);
std::size_t endpos = s.find(to);
unsigned int l = endpos-startpos-2;
s.replace(startpos+2, endpos, l, '*');
std::cout << s;
}
The output I got is He*****
, but I wanted and expected He*****ld
.
What did I get wrong?