標籤:c c++基礎 c++ 字元流 編程
string類的尋找函數: int find(char c, int pos = 0) const;//從pos開始尋找字元c在當前字串的位置int find(const char *s, int pos = 0) const;//從pos開始尋找字串s在當前串中的位置int find(const char *s, int pos, int n) const;//從pos開始尋找字串s中前n個字元在當前串中的位置int find(const string &s, int pos = 0) const;//從pos開始尋找字串s在當前串中的位置//尋找成功時返回所在位置,失敗返回string::npos的值 int rfind(char c, int pos = npos) const;//從pos開始從後向前尋找字元c在當前串中的位置int rfind(const char *s, int pos = npos) const;int rfind(const char *s, int pos, int n = npos) const;int rfind(const string &s,int pos = npos) const;//從pos開始從後向前尋找字串s中前n個字元組成的字串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值 int find_first_of(char c, int pos = 0) const;//從pos開始尋找字元c第一次出現的位置int find_first_of(const char *s, int pos = 0) const;int find_first_of(const char *s, int pos, int n) const;int find_first_of(const string &s,int pos = 0) const;//從pos開始尋找當前串中第一個在s的前n個字元組成的數組裡的字元的位置。尋找失敗返回string::npos int find_first_not_of(char c, int pos = 0) const;int find_first_not_of(const char *s, int pos = 0) const;int find_first_not_of(const char *s, int pos,int n) const;int find_first_not_of(const string &s,int pos = 0) const;//從當前串中尋找第一個不在串s中的字元出現的位置,失敗返回string::npos int find_last_of(char c, int pos = npos) const;int find_last_of(const char *s, int pos = npos) const;int find_last_of(const char *s, int pos, int n = npos) const;int find_last_of(const string &s,int pos = npos) const; int find_last_not_of(char c, int pos = npos) const;int find_last_not_of(const char *s, int pos = npos) const;int find_last_not_of(const char *s, int pos, int n) const;int find_last_not_of(const string &s,int pos = npos) const;//find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從後向前尋找
下面附上一個執行個體:
#include<iostream>#include<string>using namespace std;void main(){ string a="0AA00AA00"; char str[]="AA"; int pos=a.find(str,2); cout<<pos<<endl;}
[C++]string類的尋找函數