文章目录

  • 1 find()函数
  • 2 find_fisrt_of()函数
  • 3 find_fisrt_not_of()函数

1 find()函数

功能:可以在指定字符串中查找完全匹配子串的位置;

示例:

// string::find
#include <iostream>       // std::cout
#include <string>         // std::stringint main ()
{std::string str ("There are two needles in this haystack with needles.");std::string str2 ("needle");// different member versions of find in the same order as above:std::size_t found = str.find(str2);if (found!=std::string::npos)std::cout << "first 'needle' found at: " << found << '\n';found=str.find("needles are small",found+1,6);//寻找的字符串来自于“needles are small”中的前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';// let's replace the first 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.

2 find_fisrt_of()函数

功能:查找在字符串中第一个与子串中的某个字符匹配的字符,返回它的位置,如果没找到就返回string::npos;

示例:

// string::find_first_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_tint main ()
{std::string str ("Please, replace the vowels in this sentence by asterisks.");//asterisks,即'*'号std::size_t found = str.find_first_of("aeiou");while (found!=std::string::npos){str[found]='*';found=str.find_first_of("aeiou",found+1);}std::cout << str << '\n';return 0;
}

运行结果:

Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.

3 find_fisrt_not_of()函数

功能:返回在字符串中首次出现的不匹配子串任何字符的首字符索引, 从index开始搜索, 如果全部匹配则返回string::npos;

示例:

// string::find_first_not_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_tint main ()
{std::string str ("look for non-alphabetic characters...");std::size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");if (found!=std::string::npos){std::cout << "The first non-alphabetic character is " << str[found];std::cout << " at position " << found << '\n';}return 0;
}

运行结果:

The first non-alphabetic character is - at position 12

以上内容来自:

  1. Scott_dingg_string 中的 find()、 find_first_of()、find_first_not_of()
  2. www.cplusplus.com

C++/C--string中的find()、find_first_of()、find_first_not_of()【转载】相关推荐

  1. string中find和find_first_of函数用法总结

    一:find 函数原型: size_t find ( const string& str, size_t pos = 0 ) const; size_t find ( const char* ...

  2. C++string中find_first_not_of()函数和find_last_not_of()函数

    C++string中find_first_not_of()函数和find_last_not_of()函数 C++string中find_first_not_of()函数和find_last_not_o ...

  3. std::string中的find_first_of()和find_last_of()函数

    编程语言: c++/linux 在std::string中,有时需要找到一个string中最后一个或者第一个以某个特定的字符开始的位置或者下标,这时就需要使用find_first_of()和find_ ...

  4. C++ string中find ,rfind 等函数 用法总结及示例

    string中 find()的应用  (rfind() 类似,只是从反向查找) 原型如下: (1)size_t find (const string& str, size_t pos = 0) ...

  5. C++string中用于查找的find系列函数浅析

    https://www.cnblogs.com/zpcdbky/p/4471454.html 总述:       以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即 ...

  6. C++string中的find()函数

    C++ string中的find()函数 查找字符串s1中是否包含子串s2? 思路:此处需要用到string库中的find函数与npos参数. (1)string::npos参数 string::np ...

  7. C++String中的find用法

    #include<string> string 是c++中一个非常重要函数. 在处理字符串的时候经常用到. find是string中一个查找函数. find用法: 1.find() 示例: ...

  8. string中的find函数

    今天看代码,发现对STL中find函数掌握还是有点少,现在总结一下find函数的用法. 在非string类型的容器里,可以直接找出所对应的元素.find函数需要几个参数:迭代器.下标志.所要找的元素. ...

  9. c++中string中find用法(快速入门)

    int strStr(string s1, string s2) { if(s1.find(s2)!=s1.npos)return s1.find(s2); return -1; } string中f ...

  10. string中的Copy-on-Write技术

        在谈这项技术之前,我们先来了解一下string类内存分配.string类有一个私有成员,其类型是一个char*,记录用户从堆上分配内存的地址,其在构造时分配内存,在析构时释放内存.因为是从堆上 ...

最新文章

  1. AI产品经理的定义和分类
  2. KBMMW 4.81.00 发布
  3. 采用FTP协议实现文件的上传
  4. 蜘蛛日志分析工具_如何分析蜘蛛日志?
  5. 笔记本电脑风扇声音大怎么办_车子才开几年发动机就发出哒哒哒的声音怎么办?不一定是大问题_搜狐汽车...
  6. mybatisplus page排序_PostgreSQL使用WITH xxx AS()查询,使用Page中的OrderItem排序,会把WITH xxx AS()这段SQL忽略导致报错...
  7. xlwings,让excel飞起来!
  8. OPNET网络仿真分析-目 录
  9. 女生学java软件开发怎么样?就业前景如何?
  10. 全站仪 经纬仪 水准仪 操作演示视频教程 建筑工程测量放线7日通
  11. 全能视频播放器:OmniPlayer for Mac(1.4.6)
  12. 深度学习论文阅读目标检测篇(六)中英对照版:YOLOv3《 An Incremental Improvement》
  13. 新装的服务器发布iis网站后提示500.19错误代码:0x8007000d问题的解决过程
  14. Linux下输出大字、艺术字--figlet
  15. 【word操作】论文页眉页脚设置
  16. Linux之编写shell脚本
  17. 采集天猫网的10个经典方法
  18. EXTJS 6 Grid 滚动到底部 触发事件(如:加载数据)
  19. 如何开发ABAP报表程序(具体例子)
  20. Rejecting re-init on previously-failed class java.lang.Class<org.apache.commons.httpclient.HttpClien

热门文章

  1. MySQL数据库同步神器 - Gravity - 比Datax好用
  2. 面试官系统精讲Java源码及大厂真题 - 48 一起看过的 Java 源码和面试真题
  3. 容器编排技术 -- Kubernetes kubectl create secret 命令详解
  4. Oracle11gR2下搭建DataGuard主备同步详解
  5. OSPF——虚链路详解(含配置命令)
  6. 【Linux系列】Linux基础知识整理
  7. cookie的设置与取值
  8. java 从键盘中读取字符流 自定义异常
  9. 思维导图网页版、临时使用推荐工具——画图本
  10. 【C语言】创建一个函数,将输入的2个数排序