一、定义

string (1)
size_t find (const string& str, size_t pos = 0) const;
c-string (2)
size_t find (const char* s, size_t pos = 0) const;
buffer (3)
size_t find (const char* s, size_t pos, size_t n) const;
character (4)
size_t find (char c, size_t pos = 0) const;
查找字符串中第一次出现 str 的位置并返回。当参数pos被指定时,查找只从pos的位置及其后进行,忽略字符串中pos之前的字符
不同于find_first_of方法,当需要查找的参数为多个字符时,必须全部匹配,才返回pos

二、参数

str
需要查找的字符串
pos
从被搜索字符串的第几个字符开始搜索
如果大于被查找字符串的长度,将不会匹配到
注意:字符串的位置从0开始计数
s
指向字符数组的指针
如果指定了参数n (3), 只匹配数组中的前n个字符
另外 (2), 支持空字符结尾的字符串: 待查找字符的长度取决于第一个出现的空字符
n
需要匹配序列的前几个字符
c
被查找的单个字符

三、返回值

第一次匹配到的字符位置,如果没找到,返回 string::npos

size_t 是无符号整形,等同于string::size_type

示例:

// string::find
#include <iostream> // std::cout
#include <string> // std::string int main ()
{
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle"); // 如下调用了两种find方法:
 //直接查找字符串"needle" ----string(1)
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';
 // 从found+1的位置开始查找,6代表只查找"needles are small"的前6个即"needle" ---buffer(3)
found=str.find("needles are small",found+1,6);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n'; found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n'; found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n'; // 替换第一个"needle"
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '\n'; return 0;
}

运行结果:

first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles.

[C++]String::find的更多相关文章

  1. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  2. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

  3. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  4. [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密

    string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...

  5. js报错: Uncaught RangeError: Invalid string length

    在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...

  6. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  7. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  8. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  9. 在多线程编程中lock(string){...}隐藏的机关

    常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...

  10. BCL中String.Join的实现

    在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...

随机推荐

  1. 4--Selenium环境准备---chromedriver.exe 与chrome版本匹配

    0.jdk8 和eclipse 4.6 https://www.eclipse.org/downloads/packages/release/neon/3 1.selenium-server-stan ...

  2. 2016 多校联赛7 Elegant Construction

    Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, ...

  3. git之自学

  4. [JetBrains注册] 利用教育邮箱注册JetBrains产品(pycharm、idea等)的方法

    我们在使用JetBrains的一些产品时,大多使用网上的一些key去注册或者pojie的,但是由于提供这些key的服务器并不能保证稳定可用,所以可能一段时间我们使用的ide又需要重新pojie. 这里 ...

  5. 登陆网页模板 - 1 (HTML+CSS)

    一个用HTML和CSS写的简单登录页面,主要是用CSS来进行修饰美化的 这个登陆界面有输入账号和密码的表单,还有登陆和注册两个按键,点击按键分别会输出“您已成功登陆,稍后会跳转到您需要的页面~”,“您 ...

  6. 阿里云CentOS中vsftp安装、配置、卸载

    1--卸载 查看当前服务器中的vsftpdrpm -qa|grep vsftpd 例如结果为:vsftpd-2.2.2-13.el6_6.1.x86_64执行卸载rpm -e vsftpd-2.2.2 ...

  7. Windows编程___创建窗口

    创建Windows窗口不难,可以简要的概括为: 1,# 注册一个窗口类 填充WNDCLASS结构 书写窗口消息处理函数WinProc 2,# 创建一个窗口 填写基本的窗口信息 3,# 显示窗口 4,# ...

  8. directive例子2

    (function() { angular.module('app.widgets') .directive('bsModalPlus', function($window, $sce, $modal ...

  9. FZU软工第十一次作业-软件产品案例分析

    目录 前言: 第一部分.调研,评测: 1.1.初次感觉: 1.2.企业号bug: 1.3.你觉得为什么这个产品组的人没有发现这些bug: 1.4.假设你们团队需要开发这套系统,需要注意哪些方面: 2. ...

  10. #include<bits/stdc++.h>的使用

    #include<bits/stdc++.h>包含了C++里面所有的库函数,因此在写任何程序的时候只需要加上#include<bits/stdc++.h>即可.