find

提供:cppreference.com
移動: 案内, 検索

Syntax:

    #include <string>
    size_type find( const string& str, size_type index = 0 ) const;
    size_type find( const charT* str, size_type index = 0 ) const;
    size_type find( const charT* str, size_type index, size_type length ) const;
    size_type find( charT ch, size_type index = 0 ) const;

find()関数は、いずれかの値を返します。

  1. string型の"str"が現在の文字列から最初に見つかった位置を返します。検索の開始位置は"index"で与えることができ、もし見つからなかった場合は、"string::npos"を返します。
  2. char型配列の"が現在の文字列から最初に見つかった位置を返します。検索の開始位置は"index"で与えることができ、もし見つからなかった場合は、"string::npos"を返します。

例:

	string str1( "Alpha Beta Gamma Delta" );
	string::size_type loc1 = str1.find( "Omega", 0 );
	string::size_type loc2 = str1.find( "Gamma", 0 );
	if( loc1 != string::npos ) {
		cout << "Found Omega at " << loc1 << endl;
	} else {
		cout << "Didn't find Omega" << endl;
	}
	if( loc2 != string::npos ) {
		cout << "Found Gamma at " << loc2 << endl;
	} else {
		cout << "Didn't find Gamma" << endl;
	}

結果

Didn't find Omega
Found Gamma at 11

Related Topics: find_first_not_of, find_first_of, find_last_not_of, find_last_of, rfind

個人用ツール
名前空間
変種
操作
案内
ツールボックス
他の言語