「C++」rfind()メソッドで文字列の後ろから検索するサンプル

説明
rfindは最後に出現した位置を返します。
対象文字列.rfind(検索する文字列)
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
include <iostream>
using namespace std;
int main() {
string target = "arkgame-study-skill";
cout << target.rfind("a") << endl;
cout << target.rfind("game") << endl;
cout << target.rfind("ark") << endl;
cout << target.rfind("skill") << endl;
cout << target.rfind("-") << endl;
if (target.rfind("h") == string::npos) {
cout << "存在しません" << endl;
}
return 0;
}
include <iostream> using namespace std; int main() { string target = "arkgame-study-skill"; cout << target.rfind("a") << endl; cout << target.rfind("game") << endl; cout << target.rfind("ark") << endl; cout << target.rfind("skill") << endl; cout << target.rfind("-") << endl; if (target.rfind("h") == string::npos) { cout << "存在しません" << endl; } return 0; }
include <iostream>
using namespace std;


int main() {
      string target = "arkgame-study-skill";

      cout << target.rfind("a") << endl;
      cout << target.rfind("game") << endl; 
      cout << target.rfind("ark") << endl; 
      cout << target.rfind("skill") << endl;
      cout << target.rfind("-") << endl; 
    
      if (target.rfind("h") == string::npos) {
            cout << "存在しません" << endl;
      }
    return 0;
}

実行結果
4
3
0
14
13
存在しません

C++

Posted by arkgame