我想*
字符串中替換字符串,使用此代碼在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 –