字符串的查找刪除---C++中string.find()函數與string::npos

給定一個短字符串(不含空格),再給定若干字符串,在這些字符串中刪除所含有的短字符串ios

輸入:c++

輸入只有一組數據函數

輸入一個短字符串(不含空格),再輸入若干字符串直到文件結束爲止spa

輸出:code

刪除輸入的短字符串(不區分大小寫)並去掉空格htm

#include <stdio.h>
#include <string>
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
    char str[101];
    gets(str); //輸入短字符串
    string a = str;
    for(int i = 0; i<a.size(); ++i)
    { //將a中字符串所有改成小寫字母
        a[i] = tolower(a[i]);
    }
    while(gets(str))
    {
        string b = str,c = b; //將字符串保存至b,c,b將用於保存小寫化後的字符,c保存原字符串
        for(int i = 0; i<b.size(); ++i)
        { //將b中的字符所有改成小寫,便於匹配
            b[i] = tolower(b[i]);
        }
        int t = b.find(a,0); //在b串中查找a的位置,返回索引
        while(t != string::npos) //查找成功,則重複循環
        {
            c.erase(t,a.size());
            b.erase(t,a.size());
            t = b.find(a,t); //從t位置爲起點繼續查找b中下一個出現字符串a的位置
        }
        t = c.find('',0);
        while(t != string::npos)
        { //刪除c中全部空格
            c.erase(t,1);
            t = c.find(' ',0);
        }
        cout<<c<<endl;
    }
    return 0;
}

可見,在使用了string對象後,關於字符串處理的問題將獲得大大的簡化,注意,因爲要求中「大小寫不區分」,因此咱們先將字符串所有改成小寫後進行匹配,tolower函數,於對象

C標準函數庫中的頭文件 ctype.h中blog

注意:索引

查找字符串a是否包含子串b,不是用strA.find(strB) > 0 而是 strA.find(strB) != string:npos字符串

其中string:npos是個特殊值,說明查找沒有匹配


string::size_type pos = strA.find(strB);
if(pos != string::npos){}
-------------------------------------------
int idx = str.find("abc");
if (idx == string::npos)
...
上述代碼中,idx的類型被定義爲int,這是錯誤的,即便定義爲 unsigned int 也是錯的,它必須定義爲 string::size_type。
npos 是這樣定義的:
static const size_type npos = -1;

由於 string::size_type (由字符串配置器 allocator 定義) 描述的是 size,故需爲無符號整數型別。由於缺省配置器以型別 size_t 做爲 size_type,因而 -1 被轉換爲無符號整數型別,npos 也就成了該型別的最大無符號值。不過實際數值仍是取決於型別 size_type 的實際定義。不幸的是這些最大值都不相同。事實上,(unsigned long)-1 和 (unsigned short)-1 不一樣(前提是二者型別大小不一樣)。所以,比較式 idx == string::npos 中,若是 idx 的值爲-1,因爲 idx 和字符串string::npos 型別不一樣,比較結果可能獲得 false。
要想判斷 find() 的結果是否爲npos,最好的辦法是直接比較:

if (str.find("abc") == string::npos) { ... }

錯誤:if(str.find("abc") ) 
注:找不到abc會返回-1,不爲0爲True。0爲False 

一般來講,find函數用於尋找某個序列的在string中第一次出現的位置。

find函數有如下四種重載版本:

size_t find (const string& str, size_t pos = 0) const noexcept;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_type n) const;
size_t find (char c, size_t pos = 0) const noexcept;
第一個參數老是被搜尋的對象。
第二個參數指出string內的搜索起點(索引)。
第三個參數指出搜尋的字符個數。


////find函數返回類型 size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;

//find 函數 返回jk 在s 中的下標位置 
position = s.find("jk");
 if (position != s.npos)  //若是沒找到,返回一個特別的標誌c++中用npos表示,我這裏npos取值是4294967295,
 {
  cout << "position is : " << position << endl;
 }
 else
 {
  cout << "Not found the flag" + flag;
 } 


//find 函數 返回flag 中任意字符 在s 中第一次出現的下標位置
 flag = "c";
 position = s.find_first_of(flag);
 cout << "s.find_first_of(flag) is : " << position << endl;

 //從字符串s 下標5開始,查找字符串b ,返回b 在s 中的下標
 position=s.find("b",5);
 cout<<"s.find(b,5) is : "<<position<<endl;

//查找s 中flag 出現的全部位置。
 flag="a";
 position=0;
 int i=1;
 while((position=s.find_first_of(flag,position))!=string::npos)
 {
  //position=s.find_first_of(flag,position);
  cout<<"position  "<<i<<" : "<<position<<endl;
  position++;
  i++;
 }

 //查找flag 中與s 第一個不匹配的位置
 flag="acb12389efgxyz789";
 position=flag.find_first_not_of (s);
 cout<<"flag.find_first_not_of (s) :"<<position<<endl;


 //反向查找,flag 在s 中最後出現的位置
 flag="3";
 position=s.rfind (flag);
 cout<<"s.rfind (flag) :"<<position<<endl;
}
相關文章
相關標籤/搜索