我想*
字符串中替换字符串,使用此代码在helloworld
更换he
和ld
之间的一切:std :: string替换不保留结束?
#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;
}
我得到的输出为He*****
,但我希望和预期He*****ld
。 我得到了什么错误?
请提供[最小,完整和可验证示例](https://stackoverflow.com/help/mcve)。 – InternetAussie
请提供你已经尝试过的! – Sumeet
也许这可能会帮助你看起来类似于你的问题: https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string –