私は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 –