原文链接

#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,这是一个很大的数,其值不需要知道。

string find简析的更多相关文章

  1. RecycleView + CardView 控件简析

    今天使用了V7包加入的RecycleView 和 CardView,写篇简析. 先上效果图: 原理图: 这是RecycleView的工作原理: 1.LayoutManager用来处理RecycleVi ...

  2. Android 启动过程简析

    首先我们先来看android构架图: android系统是构建在linux系统上面的. 所以android设备启动经历3个过程. Boot Loader,Linux Kernel & Andr ...

  3. Android RecycleView + CardView 控件简析

    今天使用了V7包加入的RecycleView 和 CardView,写篇简析. 先上效果图: 原理图: 这是RecycleView的工作原理: 1.LayoutManager用来处理RecycleVi ...

  4. Java Annotation 及几个常用开源项目注解原理简析

    PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotation.key 一.Annotation 示 ...

  5. 【ACM/ICPC2013】POJ基础图论题简析(一)

    前言:昨天contest4的惨败经历让我懂得要想在ACM领域拿到好成绩,必须要真正的下苦功夫,不能再浪了!暑假还有一半,还有时间!今天找了POJ的分类题库,做了简单题目类型中的图论专题,还剩下二分图和 ...

  6. SimpleDateFormat使用简析

    title: SimpleDateFormat使用简析 date: 2016-07-11 11:48:20 tags: Java SimpleDateFormat --- [转载自博客:http:// ...

  7. zxing二维码扫描的流程简析(Android版)

    目前市面上二维码的扫描似乎用开源google的zxing比较多,接下去以2.2版本做一个简析吧,勿喷... 下载下来后定位两个文件夹,core和android,core是一些核心的库,android是 ...

  8. AFNetworking封装思路简析

    http://blog.csdn.net/qq_34101611/article/details/51698473 一.AFNetworking的发展 1. AFN 1.0版本 AFN 的基础部分是 ...

  9. [转载] Thrift原理简析(JAVA)

    转载自http://shift-alt-ctrl.iteye.com/blog/1987416 Apache Thrift是一个跨语言的服务框架,本质上为RPC,同时具有序列化.发序列化机制:当我们开 ...

随机推荐

  1. OCR 维护 crsd.log

    ###########sample 1 OCR corruption messages are reported in crsd.log, automatic OCR backup is failin ...

  2. Mysql 游标的定义与使用方式

    创建游标: 首先在MySql中创建一张数据表: CREATE TABLE IF NOT EXISTS `store` (   `id` int(11) NOT NULL AUTO_INCREMENT, ...

  3. android Activity启动过程(四)startActivityUncheckedLocked

    final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteract ...

  4. Unity GetComponentsInChildren

    1 Component.GetComponentsInChildren 和 GameObject.GetComponentsInChildren 一样吗?  API上解释一样. 2. //拿到游戏对象 ...

  5. python3+Appium自动化12-H5元素定位环境搭建

    前言 在混合开发的App中,经常会有内嵌的H5页面.那么这些H5页面元素该如何进行定位操作呢? 针对这种场景直接使用前面所讲的方法来进行定位是行不通的,因为前面的都是基于Andriod原生控件进行元素 ...

  6. 深入学习webpack(二)

    深入学习webpack(二) 在深入学习webpack(一)中,我通过一个例子介绍了webpack的基本使用方法,下面将更为系统的学习webpack的基本概念,对于一门技术的掌握我认为系统化还是很重要 ...

  7. Numpy的那些事儿

    2 NumPy-快速处理数据 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1, ...

  8. stm32 定时器初步

    今天学习STM32 的通用定时器:TIMx (TIM2.TIM3.TIM4 和 TIM5). 定时器比较复杂,功能繁多,这里我们就说最基本的计数功能. 1.定时器的时钟从哪里来? 定时器的时钟来源有 ...

  9. mysql 将null转代为0(转)

    1.如果为空返回0 select ifnull(null,0) 2.如果为空返回0,否则返回1 select if(isnull(col),0,1) as col. MYSQL 中的IFNULL函数 ...

  10. td标签里内容不换行

    在一些页面开发中,除自己操作外,引起换行的情况一般有: Ex一.td标签里内容长度过长引起换行: Ex二.div标签(或其他标记)里内容有文本和图片引起换行: 解决方法: 针对例子一用<nobr ...