C 中查詢字元或者字串的一些函式

2021-08-24 17:48:08 字數 3190 閱讀 5339

string類的標頭檔案提供了很多搜尋相關的函式比如find()函式及其變體。這使得我們可以以多種不同的方式在字串中搜尋給定的子字串或字元。但是對於初學者來講,經常被這些長相類似的函式所混淆。 

下面總結了string類的find相關函式:

應用例句:

int main(int argc, char *argv)

find函式有四種變體:

方法原型

描述size_type find(const string & str, size_type pos = 0) const

從字串的pos位置開始,查詢子字串str。如果找到,則返回該子字串首次出現時其首字元的索引;否則,返回string::npos

size_type find(const char * s, size_type pos = 0) const

從字串的pos位置開始,查詢子字串s。如果找到,則返回該子字串首次出現時其首字元的索引;否則,返回string::npos

size_type find(const char * s, size_type pos = 0, size_type n) const

從字串的pos位置開始,查詢s的前n個字元組成的子字串。如果找到,則返回該子字串首次出現時其首字元的索引;否則,返回string::npos

size_type find(const char ch, size_type pos = 0) const

從字串的pos位置開始,查詢字元ch。如果找到,則返回該子字串首次出現的位置;否則,返回string::npos

p.s.string::npos是字串可儲存的最大字元數,通常是無符號int或無符號long的最大取值。

string庫還提供了相關的方法:rfind(),find_first_of(),find_last_of(),find_first_not_of(),find_last_not_of()。他們的過載函式特徵標都與find()方法相同。

原型:

size_type rfind(const string & str, size_type pos = npos) const;

size_type rfind(const char * s, size_type pos = npos) const;

size_type rfind(const char * s, size_type pos = npos, size_type n) const;

size_type rfind(const char ch, size_type pos = npos) const;

rfind()方法查詢子字串或字元最後一次出現的位置,與find不同的是rfind是從後向前查詢

原型:

size_type find_first_of(const string & str, size_type pos = 0) const;

size_type find_first_of(const char * s, size_type pos, size_type n) const;

size_type find_first_of(const char * s, size_type pos = 0) const;

size_type find_first_of(char c, size_type pos = 0) const;

find_first_of()方法在字串中查詢引數中任何乙個字元首次出現的位置。例如,下面的語句返回r在」cobra」中的位置(即索引3),因為這個」hark」中各個字母在」cobra」首次出現的位置:

string snake1 = "cobra";

int where = snake1.find_first_of("hark");

原型:

size_type find_last_of(const string & str, size_type pos = npos) const;

size_type find_last_of(const char * s, size_type pos, size_type n) const;

size_type find_last_of(const char * s, size_type pos = npos) const;

size_type find_last_of(char c, size_type pos = npos) const;

find_last_of()方法在字串中查詢引數中任何乙個字元最後一次出現的位置。

原型:

size_type find_first_not_of(const string & str, size_type pos = 0) const;

size_type find_first_not_of(const char * s, size_type pos, size_type n) const;

size_type find_first_not_of(const char * s, size_type pos = 0) const;

size_type find_first_not_of(char c, size_type pos = 0) const;

find_first_not_of()方法在字串中查詢第乙個不包含在引數中的字元,因此下面的語句返回c在」cobra」中的位置,因為」hark」中沒有c:

string snake1 = "cobra";

int where = snake1.find_first_not_of("hark");

原型:

size_type find_last_not_of(const string & str, size_type pos = npos) const;

size_type find_last_not_of(const char * s, size_type pos, size_type n) const;

size_type find_last_not_of(const char * s, size_type pos = npos) const;

size_type find_last_not_of(char c, size_type pos = npos) const;

find_last_not_of()方法在字串中查詢最後乙個不包含在引數中的字元。

查詢字串中字元位址

這個函式引數中的陣列array是以 0結束的字串,要求在字串array中查詢出第乙個與引數search給出的字元相同的字元。如果找到,通過第三個引數 pa 返回array字串中首先碰到的search字元的位址。如果沒找到,則pa為null。include include include includ...

C語言查詢字串

使用c語言實現字串查詢子串,返回第乙個子串出現的位置。ubuntu16.04 lts gcc編譯器 include define n 100 int find str char str1,char str2 j 0 return flag int main else return 0 函式原型 in...

python在字串中查詢字元

兩類函式 find rfind index rindex 找到了都返回下標.find找不到返回 1,index找不到丟擲valueerror.帶r的表示從右向左找.都可以使用第二個引數表示從哪個下標開始找.a abcdab a.find a out 3 0 a.rfind a out 4 4a.rf...