「C++入門」replaceメソッドで指定位置の文字列を置き換えるサンプル

2021年2月28日

書式
対象文字列.replace(開始位置,長さ,置換する文字列)
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
using namespace std;
int main() {
string strA = "arkgame";
cout << "置き換える前文字列:" << endl;
cout << strA<< endl;
cout << "置き換える後文字列:" << endl;
cout << strA.replace(0,3, "skill") << endl;
return 0;
}
#include <iostream> using namespace std; int main() { string strA = "arkgame"; cout << "置き換える前文字列:" << endl; cout << strA<< endl; cout << "置き換える後文字列:" << endl; cout << strA.replace(0,3, "skill") << endl; return 0; }
#include <iostream>
using namespace std;


int main() {
      string strA = "arkgame";
    cout << "置き換える前文字列:" << endl;
    cout << strA<< endl; 
    cout << "置き換える後文字列:" << endl;
      cout << strA.replace(0,3, "skill") << endl; 
      return 0;
}

実行結果
置き換える前文字列:
arkgame
置き換える後文字列:
skillgame

C++

Posted by arkgame