字符串的查找删除---C++中string.find()函数与string::npos
给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串
输入:
输入只有一组数据
输入一个短字符串(不含空格),再输入若干字符串直到文件结束为止
输出:
删除输入的短字符串(不区分大小写)并去掉空格
#include <stdio.h>
#include <string>
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
char str[];
gets(str); //输入短字符串
string a = str;
for(int i = ; 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 = ; i<b.size(); ++i)
{ //将b中的字符全部改为小写,便于匹配
b[i] = tolower(b[i]);
}
int t = b.find(a,); //在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('',);
while(t != string::npos)
{ //删除c中所有空格
c.erase(t,);
t = c.find(' ',);
}
cout<<c<<endl;
}
return ;
}
可见,在使用了string对象后,关于字符串处理的问题将得到大大的简化,注意,由于要求中“大小写不区分”,所以我们先将字符串全部改为小写后进行匹配,tolower函数,于
注意:
查找字符串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 = ) const noexcept;
size_t find (const char* s, size_t pos = ) const;
size_t find (const char* s, size_t pos, size_type n) const;
size_t find (char c, size_t pos = ) 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",);
cout<<"s.find(b,5) is : "<<position<<endl; //查找s 中flag 出现的所有位置。
flag="a";
position=;
int i=;
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="";
position=s.rfind (flag);
cout<<"s.rfind (flag) :"<<position<<endl;
}
字符串的查找删除---C++中string.find()函数与string::npos的更多相关文章
- C++中string::find()函数和string::npos函数的使用
1. string::find()函数和string::npos函数的介绍 我们在学习C++的时候必不可少的使用到string类中的find()函数,它是一个查找函数,功能还是很强大的,但是此处我们不 ...
- C++中string.find()函数,string.find_first_of函数与string::npos
查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA. ...
- C++中string.find()函数与string::npos
先说说string::npos参数: npos 是一个常数,用来表示不存在的位置,类型一般是std::container_type::size_type 许多容器都提供这个东西.取值由实现决定,一般是 ...
- 九度OJ 1168:字符串的查找删除 (查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4276 解决:1699 题目描述: 给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串. 输入: 输入只有1 ...
- C# 中奇妙的函数–8. String Remove() 和 Replace()
http://www.cnblogs.com/multiplesoftware/archive/2011/09/27/2192710.html 当对字符串进行操作时,我们经常要删除或者是替换一部分子字 ...
- C# 中奇妙的函数–7. String Split 和 Join
很多时候处理字符串数据,比如从文件中读取或者存入 - 我们可能需要加入分隔符(如CSV文件中的逗号),或使用一个分隔符来合并字符串序列. 很多人都知道使用split()的方法,但使用与其对应的Join ...
- Go语言中字符串的查找方法小结
这篇文章主要介绍了Go语言中字符串的查找方法小结,示例的main函数都是导入strings包然后使用其中的方法,需要的朋友可以参考下 1.func Contains(s, substr strin ...
- C语言:将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。-删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)-在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,
//将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换. #include <stdio.h> #include <string.h> void fun ...
- C语言:假定输入的字符串只包含字母和*号,fun函数:除了尾部的*号以外,将字符的其他*号进行全部删除,形参p已经指向字符串中最后一个字母。-利用折半查找整数m在有序数组中的位置,若找到,返回下标值,否则返回-1。
//假定输入的字符串只包含字母和*号,fun函数:除了尾部的*号以外,将字符的其他*号进行全部删除,形参p已经指向字符串中最后一个字母. #include <stdio.h> void f ...
随机推荐
- rac ASM下最简单归档开启/关闭方法
原创作品,出自 “深蓝的blog” 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/47172639本次先来介绍一下在rac环 ...
- selenium - 三种元素等待
1.sleep 休眠方法 sleep()由python的time模块提供. 当执行到sleep()方法时,脚本会定时休眠所设置的时长,sleep()方法默认参数是s(秒),sleep(2) 表示休眠2 ...
- 生产环境连接数据库失败:Cannot create PoolableConnectionFactory❨Got mins one from a read call❩
生产环境发现有接口调不通,而且集中在两个节点,其他节点都没问题.抓取日志发现报错如下: Context initialization failed. org.springframework. bean ...
- C语言通过地址传递参数
// 正确 #include <stdio.h> struct para { int a; int b; }; struct para test = { .a = , .b = , }; ...
- erlang的dict和maps模块
erlang在r17以后增加了map这个数据结构,在之前,类似map的需求用dict模块来实现,这里直接贴一下相关的操作 dict D = dict:new(). D1 = dict:store(k1 ...
- Jmeter录制HTTPS 补充
Jmeter有录制功能,录制HTTPs需要增加一个证书配置,录制步骤如下: 1.打开jmeter,添加线程组.线程组右键,逻辑控制器>录制控制器 工作台 右键 非测试元件 >HTTP代理服 ...
- python中的enumerate函数用于遍历序列中的元素以及它们的下标
enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')): print i,j 0 a1 b2 c ...
- Sqoop导出MySQL数据
导入所有表: sqoop import-all-tables –connect jdbc:mysql://ip:port/dbName --username userName --password p ...
- jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 我在使用$.parseJSON解析后 ...
- cocoa 线程操作
在Cocoa 中创建线程使用NSThread类的detachNewThreadSelector: toTarget:withObject:方法 NSPort *port1 = [NSPort port ...