「C++」指定位置の文字列を置き換えるサンプル

書式
対象文字列.replace(開始位置,長さ,置換文字列)
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
using namespace std;
int main() {
string strA = "ttt-ss";
cout <<"result1:" <<endl;
cout << strA.replace(1,2,"abc") << endl;
string strB = "eee-fff";
cout <<"result2: "<<endl;
cout << strB.replace(0,4,"def") << endl;
return 0;
}
#include <iostream> using namespace std; int main() { string strA = "ttt-ss"; cout <<"result1:" <<endl; cout << strA.replace(1,2,"abc") << endl; string strB = "eee-fff"; cout <<"result2: "<<endl; cout << strB.replace(0,4,"def") << endl; return 0; }
#include <iostream>
using namespace std;

int main() {

      string strA = "ttt-ss";
    cout <<"result1:" <<endl;
      cout << strA.replace(1,2,"abc") << endl; 

      string strB = "eee-fff";
    cout <<"result2: "<<endl;
      cout << strB.replace(0,4,"def") << endl; 

      return 0;
}

実行結果
result1:
tabc-ss
result2:
deffff

C++

Posted by arkgame