原文链接

#include <string>
#include <iostream>
using namespace std;

void main()
{

  1. ////find函数返回类型 size_type
  2. string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
  3. string flag;
  4. string::size_type position;
  5. //find 函数 返回jk 在s 中的下标位置
  6. position = s.find("jk");
  7. if (position != s.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
  8. {
  9. cout << "position is : " << position << endl;
  10. }
  11. else
  12. {
  13. cout << "Not found the flag" + flag;
  14. }
  1. //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置
  2. flag = "c";
  3. position = s.find_first_of(flag);
  4. cout << "s.find_first_of(flag) is : " << position << endl;
  1. //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
  2. position=s.find("b",5);
  3. cout<<"s.find(b,5) is : "<<position<<endl;
  1. //查找s 中flag 出现的所有位置。
  2. flag="a";
  3. position=0;
  4. int i=1;
  5. while((position=s.find_first_of(flag,position))!=string::npos)
  6. {
  7. //position=s.find_first_of(flag,position);
  8. cout<<"position  "<<i<<" : "<<position<<endl;
  9. position++;
  10. i++;
  11. }
  1. //查找flag 中与s 第一个不匹配的位置
  2. flag="acb12389efgxyz789";
  3. position=flag.find_first_not_of (s);
  4. cout<<"flag.find_first_not_of (s) :"<<position<<endl;
  1. //反向查找,flag 在s 中最后出现的位置
  2. flag="3";
  3. position=s.rfind (flag);
  4. cout<<"s.rfind (flag) :"<<position<<endl;
  5. }

说明:

1.  如果string sub = ”abc“;

string s = ”cdeabcigld“;

s.find(sub) , s.rfind(sub) 这两个函数,如果完全匹配,才返回匹配的索引,即:当s中含有abc三个连续的字母时,才返回当前索引。

s.find_first_of(sub),   s.find_first_not_of(sub),   s.find_last_of(sub),  s.find_last_not_of(sub)  这四个函数,查找s中含有sub中任意字母的索引。

2.  如果没有查询到,则返回string::npos,这是一个很大的数,其值不需要知道。

最新文章

  1. 添加 All Exceptions 断点后, 每次运行都会在 main.m 中断的一种解决方法
  2. iOS 自定义图片和文字垂直显示按钮&lt;上面是图片,文字显示下面&gt;
  3. Java 工具集
  4. Java for LeetCode 231 Power of Two
  5. 创建com服务器
  6. json+servlet+ajax
  7. oracle starup报错
  8. win8下 web测试 之 hosts绑定
  9. Mps与Mrp区别
  10. html object元素
  11. 微信支付服务商模式(受理机构模式)开发注意事项,jsapi支付
  12. Nginx (二) Nginx的反向代理负载均衡以及日志切割
  13. jenkins创建pipeline
  14. PAT1112:Stucked Keyboard
  15. 2.Django路由规则
  16. Ubuntu18.04更换国内源(阿里,网易,中科大,清华等源)
  17. asp.net 使用rabbitmq事例
  18. Visual Studio 2010 集成 SP1 补丁 制作 Visual Studio 2010 Service Pack 1 完整版安装光盘的方法
  19. learning makefile 模式规则
  20. 解决Linux下Qt编译出现&quot;cannot find -lGL&quot;以及无法安装libgl1-mesa-dev的问题

热门文章

  1. java多线程(四)
  2. str 操作方法
  3. Ubuntu截图工具gnome-screenshot使用教程
  4. Kure讲HTML_HTML界面结构
  5. mysql java 通用AES加密
  6. 初步学习XML的基本代码
  7. C#入门--字段与属性
  8. deployment删除后,副本集未删除,解决之道
  9. html 01前沿-web介绍
  10. angular的多个模块执行 angular里 字符串与对象的互转