c++ string类find总结
2024-10-09 09:52:06
c++ string类的find:
1.find
string s = "My cat's breath smells like cat food.";
int a=s.find("breath");
cout<<a<<endl;
因为find是从0数起,输出字符串第一个字符的索引(从0开始),所以a为9
2.rfind
string s = "My cat's breath smells like cat food.";
int a=s.rfind("breath");
cout<<a<<endl;
因为rfind是从后部数起,还是输出字符串第一个字符的索引(从0开始)
其实,find与rfind一样,都可以找完整的字符串和字符,下面来看看另外一些奇葩的函数
3.find_first_of
string s = "My cat's breath smells like cat food.";
int a=s.find_first_of("beah");
cout<<a<<endl;
从0数起,但它只需找到一个与它串里一样的字符就输出来了
答案是:4
4.find_last_of
string s = "My cat's breath smells like cat food.";
int a=s.find_last_of("beah");
cout<<a<<endl;
从后部数起,而且和find_first_of一样,输出最后一个有一样的就好了
答案是: 29
5.find_first_not_of
string s = "aaabaaaaaabaa.";
int a=s.find_first_not_of('a');
cout<<a<<endl;
从0数起,但它只需找到一个与它串里不一样的字符就输出来了
答案是:3
6.find_last_not_of
string s = "aaabaaaaaabaa";
int a=s.find_last_not_of('a');
cout<<a<<endl;
从后部数起,而且和find_first_not_of一样,也是输出第一个(似乎是最后一个哈)有一样的就好了
答案是: 10
如果没找到的话会输出string::npos
最新文章
- Netron源码解读(一):GraphControl画布对象
- python 之sqlalchemy many to one
- NYOJ之字符串逆序输出
- 找模式串[XDU1032]
- 如何让JS的变量名变量化
- C#winform修改IP,dns
- Python基础篇-day5
- Spring Boot 实现 RabbitMQ 延迟消费和延迟重试队列
- 移动端web自适应适配布局解决方案
- React-Native采坑总结
- xlua build时 报错处理
- PCIe传输速率和可用带宽(吞吐量)计算
- SCRUM管理之KPI与OKRs结合
- paloalto防火墙注册
- java发送163邮件
- SRCNN
- Winfrom窗体无法关闭问题--检查是否存在重写
- C语言 &#183; 身份证排序
- 【BZOJ4184】shallot 线性基
- Maven 上传本地包到仓库 (来源于同事(gagahjt)的笔记本)
热门文章
- MongoDB快速入门教程 (4.1)
- 微信开发_Exception_02_";errcode";:40164,";errmsg";:";invalid ip 61.172.68.219, not in whitelist hint
- centos6.4 卸载 vim7.2 安装vim7.4
- python数据类型的72变
- lombok使用指南,代码极简工具
- 用Python语言绘制股市OBV指标效果
- 一文搞定 Spring Data JPA
- 一道题理解setTimeout,Promise,async/await以及宏任务与微任务
- Bootstrap 搭建基础页面
- JVM 专题十九:垃圾回收(三)垃圾回收相关概念