「C++」substr()で引数の開始位置から最後までの文字を取得するサンプル

2021年2月28日

書式
文字列.substr(開始位置)
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
using namespace std;
int main() {
string target = "arkgame";
cout << "位置2開始最後までの文字列:" << endl;
cout << target.substr(2) << endl;
cout << "位置3開始最後までの文字列:" << endl;
cout << target.substr(3) << endl;
cout << "位置4開始最後までの文字列:" << endl;
cout << target.substr(4) << endl;
return 0;
}
#include <iostream> using namespace std; int main() { string target = "arkgame"; cout << "位置2開始最後までの文字列:" << endl; cout << target.substr(2) << endl; cout << "位置3開始最後までの文字列:" << endl; cout << target.substr(3) << endl; cout << "位置4開始最後までの文字列:" << endl; cout << target.substr(4) << endl; return 0; }
#include <iostream>
using namespace std;


int main() {
      string target = "arkgame";
    
        cout << "位置2開始最後までの文字列:" << endl;
      cout << target.substr(2) << endl; 
    
        cout << "位置3開始最後までの文字列:" << endl;
      cout << target.substr(3) << endl; 
    
        cout << "位置4開始最後までの文字列:" << endl;
      cout << target.substr(4) << endl; 
    
      return 0;
}

実行結果
位置2開始最後までの文字列:
kgame
位置3開始最後までの文字列:
game
位置4開始最後までの文字列:
ame

C++

Posted by arkgame