C++ string 字串尋找函數

來源:互聯網
上載者:User

在寫C++程式中,總會遇到要從一個字串中尋找一小段子字串的情況,對於在C中,我們經常用到strstr()或者strchr()這兩種方法。而對於C++的string,我們往往會用到find()。

C++:#inlcude<string>

C: #include<string.h>

find():在一個字串中尋找一個指定的單個字元或字元數組。如果找到,就返回最初相符的開始位置;如果沒有尋找到匹配的內容,就返回string::npos。

find_first_of():在一個目標串中進行尋找,傳回值是第一個與指定字元組中任何字元匹配的字元位置。如果沒有尋找到匹配的內容,則返回npos。

find_last_of():在一個目標串中進行尋找,返回最後一個與指定字元組中任何字元匹配的字元位置。如果沒有尋找到匹配的內容,則返回npos。

find_first_not_of():在一個目標串中進行尋找,返回第一個與指定字元組中任何字元都不匹配的元素位置。如果找不到那樣的元素則返回npos。

find_last_not_of():在一個目標串中進行尋找,返回下標值最大的與指定字元組中任何字元都不匹配的元素的位置。若找不到那樣的元素則返回npos。

rfind():對一個串從尾至頭尋找一個指定的單個字元或字元組。如果找到,就返回最初相符的開始位置;如果沒有尋找到匹配的內容,則返回npos。

find(string, int):第一個參數用來指示要尋找的字元,第二個參數用來表示從字串的何處開始尋找子串(預設的尋找位置是0)。

舉例:字串匹配:

 代碼如下 複製代碼

#include "stdafx.h"

#include<iostream>

#include<math.h>

#include<string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

string T;//原串

string P;//模式

while(cin>>T>>P)

{

int count=0;

int begin=-1;

while((begin=T.find(P,begin+1))!=string::npos)

{

count++;

}

cout<<count<<endl;

}

int z;

cin>>z;

return 0;

}


運行結果為:

The string to search is 'Heartbeat'
Element in 'abcde' found at position 1
Element in 'abcde' found at position 2
Element in 'aeiou' found at position 6
Element in 'aeiou' found at position 1
'e' found at position 6


例子

 代碼如下 複製代碼

#include<iostream>
#include<string>

using namespace std;

int main()
{
 string s = "**Gteate Wall**!";
 string t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 cout<<"s: "<<s<<endl;
 cout<<"t: "<<t<<endl;

 int first=s.find_first_of(t);

 if(first == string::npos){
  cout<<"s中所有字元均不在t中"<<endl;
 }else {
  cout<<"s中出現在t中的字元的第一個字元:"<<s[first]<<endl;
 }

 int last = s.find_last_of(t);
 if(last == string::npos){
  cout<<"s中所有字元均不在t中"<<endl;
  return 1;
 }else {
  cout<<"s中出現在t的字元的最後一個字元:"<<s[last]<<endl;
  return 1;
 }

}


尋找

string str;
cin >> str;

str.find("ab");//返回字串 ab 在 str 的位置
str.find("ab", 2);//在 str[2]~str[n-1] 範圍內尋找並返回字串 ab 在 str 的位置
str.rfind("ab", 2);//在 str[0]~str[2] 範圍內尋找並返回字串 ab 在 str 的位置

//first 系列函數
str.find_first_of("apple");//返回 apple 中任何一個字元首次在 str 中出現的位置
str.find_first_of("apple", 2);//返回 apple 中任何一個字元首次在 str[2]~str[n-1] 範圍中出現的位置
str.find_first_not_of("apple");//返回除 apple 以外的任何一個字元在 str 中首次出現的位置
str.find_first_not_of("apple", 2);//返回除 apple 以外的任何一個字元在 str[2]~str[n-1] 範圍中首次出現的位置

//last 系列函數
str.find_last_of("apple");//返回 apple 中任何一個字元最後一次在 str 中出現的位置
str.find_last_of("apple", 2);//返回 apple 中任何一個字元最後一次在 str[0]~str[2] 範圍中出現的位置
str.find_last_not_of("apple");//返回除 apple 以外的任何一個字元在 str 中最後一次出現的位置
str.find_last_not_of("apple", 2);//返回除 apple 以外的任何一個字元在 str[0]~str[2] 範圍中最後一次出現的位置

//以上函數如果沒有找到,均返回string::npos
cout << string::npos;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.