语法:
#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
方法返回下列值中的一个:
str
第一次出现的位置,以 index
开始,若未找到,返回 string::npos
length
characters of str
within the current string, starting at index
, or string::npos
if nothing is found.For example:
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; }
The result is
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