find
Aus cppreference.com
< cpp | string | basic string
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;
Die Methode find gibt zurück eines von beiden:
- das erste Vorkommnis von str aus der aktuellen Zeichenkette, beginnend mit index oder string::npos falls nichts gefunden
- die ersten length Zeichen von str aus der aktullen Zeichenkette beginnend mit index oder string::npos falls nichts gefunden.
Zum Beispiel:
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; }
Das Ergebnis ist
Didn't find Omega Found Gamma at 11
Sieh auch: find_first_not_of, find_first_of, find_last_not_of, find_last_of, rfind