字符串的查找删除---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;
}
相关文章
相关标签/搜索