C++ string 类的 find 方法实例详解
1、C++ 中 string 类的 find 方法列表

size_type std::basic_string::find(const basic_string &__str, size_type __pos);
size_type std::basic_string::find(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find(const _CharT *__s, size_type __pos);
size_type std::basic_string::find(_CharT __c, size_type __pos);
size_type std::basic_string::rfind(const basic_string &__str, size_type __pos);
size_type std::basic_string::rfind(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::rfind(const _CharT *__s, size_type __pos);
size_type std::basic_string::rfind(_CharT __c, size_type __pos);
size_type std::basic_string::find_first_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_first_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_first_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_first_of(_CharT __c, size_type __pos);
size_type std::basic_string::find_last_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_last_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_last_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_last_of(_CharT __c, size_type __pos);
size_type std::basic_string::find_first_not_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_first_not_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_first_not_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_first_not_of(_CharT __c, size_type __pos);
size_type std::basic_string::find_last_not_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_last_not_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_last_not_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_last_not_of(_CharT __c, size_type __pos);
本文以代码和运行实例进行解析,实例中的颜色部分的规则如下:
黄色底色:在原字符串中经过了搜索,没有黄色底色的表示不在我们的搜索范围内
红色字体:搜索错误的地方
绿色字体:搜索正确
下面逐个解析
find()、
rfind()、
find_first_of()、
find_last_of()、
find_first_not_of()、
find_last_not_of()
2、find()
find函数在C++的头文件basic_string.h中有四种定义,定义如下:
2.1)第一个定义:size_type find(const _CharT* __s, size_type __pos, size_type __n) const;

/** * @brief Find position of a C substring. * @param __s C string to locate. * @param __pos Index of character to search from. * @param __n Number of characters from @a s to search for. * @return Index of start of first occurrence. * * Starting from @a __pos, searches forward for the first @a * __n characters in @a __s within this string. If found, * returns the index where it begins. If not found, returns * npos. */ size_type find(const _CharT* __s, size_type __pos, size_type __n) const;
实例代码:
1 void xx_find_3()
2 {
3 printf("%s():\n", __func__);
4 const string strSrc = "abcdefghijklmnopqrstuvwxyz";
5 const char * pStr = NULL;
6 int pos = 0;
7
8 pStr= "cdefppp";
9 pos = strSrc.find(pStr, 0, 4);
10 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
11
12 pStr= "cdefppp";
13 pos = strSrc.find(pStr, 0, 5);
14 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
15
16 pStr= "cdefppp";
17 pos = strSrc.find(pStr, 5, 5);
18 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
19 }
运行结果:
xx_find_3():
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
2.2)第二个定义:size_type find(const basic_string& __str, size_type __pos = 0) const

/** * @brief Find position of a string. * @param __str String to locate. * @param __pos Index of character to search from (default 0). * @return Index of start of first occurrence. * * Starting from @a __pos, searches forward for value of @a __str within * this string. If found, returns the index where it begins. If not * found, returns npos. */ size_type find(const basic_string& __str, size_type __pos = 0) const _GLIBCXX_NOEXCEPT { return this->find(__str.data(), __pos, __str.size()); }
实例代码:
void xx_find_1()
{
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
string strDst;
size_t pos = 0;
strDst= "abcdef";
pos = strSrc.find(strDst);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
pos = strSrc.find(strDst, 0);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
strDst = "cdef";
pos = strSrc.find(strDst, 2);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
pos = strSrc.find(strDst, 3);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
}
运行结果:
xx_find_1():
pos = 0 [find] abcdefghijklmnopqrstuvwxyz
pos = 0 [find] abcdefghijklmnopqrstuvwxyz
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
2.3)第三个定义:size_type find(const _CharT* __s, size_type __pos = 0) const

/** * @brief Find position of a C string. * @param __s C string to locate. * @param __pos Index of character to search from (default 0). * @return Index of start of first occurrence. * * Starting from @a __pos, searches forward for the value of @a * __s within this string. If found, returns the index where * it begins. If not found, returns npos. */ size_type find(const _CharT* __s, size_type __pos = 0) const { __glibcxx_requires_string(__s); return this->find(__s, __pos, traits_type::length(__s)); }
示例代码:
1 void xx_find_2()
2 {
3 printf("%s():\n", __func__);
4 const string strSrc = "abcdefghijklmnopqrstuvwxyz";
5 const char * pStr = NULL;
6 size_t pos = 0;
7
8 pStr = "cdef";
9 pos = strSrc.find(pStr);
10 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
11
12 pos = strSrc.find(pStr, 0);
13 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
14
15 pos = strSrc.find(pStr, 2);
16 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
17
18 pos = strSrc.find(pStr, 3);
19 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
20 }
运行结果:
xx_find_2():
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
2.4)第四个定义:size_type find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;

/** * @brief Find position of a character. * @param __c Character to locate. * @param __pos Index of character to search from (default 0). * @return Index of first occurrence. * * Starting from @a __pos, searches forward for @a __c within * this string. If found, returns the index where it was * found. If not found, returns npos. */ size_type find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
示例代码:
1 void xx_find_4()
2 {
3 printf("%s():\n", __func__);
4 const string strSrc = "abcdefghijklmnopqrstuvwxyz";
5 int pos = 0;
6
7 char c = 'b';
8 pos = strSrc.find(c);
9 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
10
11 pos = strSrc.find(c, 0);
12 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
13
14 pos = strSrc.find(c, 1);
15 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
16
17 pos = strSrc.find(c, 2);
18 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
19 }
运行结果:
xx_find_4():
pos = 1 [find] abcdefghijklmnopqrstuvwxyz
pos = 1 [find] abcdefghijklmnopqrstuvwxyz
pos = 1 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
3、rfind()
rfind()函数在basic_string.h中同样有四种定义,定义如下:
3.1)第一个定义:size_type rfind(const basic_string& __str, size_type __pos = npos) const _GLIBCXX_NOEXCEPT

/** * @brief Find last position of a string. * @param __str String to locate. * @param __pos Index of character to search back from (default end). * @return Index of start of last occurrence. * * Starting from @a __pos, searches backward for value of @a * __str within this string. If found, returns the index where * it begins. If not found, returns npos. */ size_type rfind(const basic_string& __str, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
实例代码:
1 void xx_rfind_1()
2 {
3 //size_type std::basic_string::rfind(const basic_string &__str, size_type __pos);
4 printf("%s():\n", __func__);
5 const string strSrc = "abcdefghijklmnopqrstuvwxyz";
6 string strDst;
7 size_t pos = 0;
8
9 strDst= "uvw";
10 pos = strSrc.rfind(strDst);
11 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
12
13 pos = strSrc.rfind(strDst, 25);
14 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
15
16 pos = strSrc.rfind(strDst, 20);
17 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
18
19 pos = strSrc.rfind(strDst, 19);
20 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
21 }
运行结果:
xx_rfind_1():
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
3.2)第二个定义:size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const;

/** * @brief Find last position of a C substring. * @param __s C string to locate. * @param __pos Index of character to search back from. * @param __n Number of characters from s to search for. * @return Index of start of last occurrence. * * Starting from @a __pos, searches backward for the first @a * __n characters in @a __s within this string. If found, * returns the index where it begins. If not found, returns * npos. */ size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const;
示例代码:
void xx_rfind_2()
{
//size_type std::basic_string::rfind(const _CharT *__s, size_type __pos, size_type __n);
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
const char * p = NULL;
size_t pos = 0;
p = "uvw";
pos = strSrc.rfind(p, 25, 2);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
pos = strSrc.rfind(p, 20, 2);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
pos = strSrc.rfind(p, 19, 2);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
}
运行结果:
xx_rfind_2():
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
3.3)第三个定义:size_type rfind(const _CharT* __s, size_type __pos = npos) const

/** * @brief Find last position of a C string. * @param __s C string to locate. * @param __pos Index of character to start search at (default end). * @return Index of start of last occurrence. * * Starting from @a __pos, searches backward for the value of * @a __s within this string. If found, returns the index * where it begins. If not found, returns npos. */ size_type rfind(const _CharT* __s, size_type __pos = npos) const
示例代码:
void xx_rfind_3() { //size_type std::basic_string::rfind(const _CharT *__s, size_type __pos); printf("%s():\n", __func__); const string strSrc = "abcdefghijklmnopqrstuvwxyz"; const char * p = NULL; size_t pos = 0; p = "uvw"; pos = strSrc.rfind(p); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 25); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 20); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 19); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); }
运行结果:
xx_rfind_3(): pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
3.4)第四个定义: size_type rfind(_CharT __c, size_type __pos = npos) const

/** * @brief Find last position of a character. * @param __c Character to locate. * @param __pos Index of character to search back from (default end). * @return Index of last occurrence. * * Starting from @a __pos, searches backward for @a __c within * this string. If found, returns the index where it was * found. If not found, returns npos. */ size_type rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
示例代码:
void xx_rfind_4() { //size_type std::basic_string::rfind(_CharT __c, size_type __pos); printf("%s():\n", __func__); const string strSrc = "abcdefghijklmnopqrstuvwxyz"; size_t pos = 0; char p = 'u'; pos = strSrc.rfind(p); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 25); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 20); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 19); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); }
运行结果:
xx_rfind_4(): pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
4、find_first_of()
find_first_of()函数也有4个定义,函数作用是在当前字符串中查找给定参数中的任意字符的第一次出现位置,有点儿拗口,下面用实例说明。
4.1)第一个定义 size_type find_first_of(const basic_string& __str, size_type __pos = 0)

1 size_type 2 find_first_of(const basic_string& __str, size_type __pos = 0) const 3 _GLIBCXX_NOEXCEPT 4 { return this->find_first_of(__str.data(), __pos, __str.size()); } 5 6 /** 7 * @brief Find position of a character of C substring. 8 * @param __s String containing characters to locate. 9 * @param __pos Index of character to search from. 10 * @param __n Number of characters from s to search for. 11 * @return Index of first occurrence. 12 * 13 * Starting from @a __pos, searches forward for one of the 14 * first @a __n characters of @a __s within this string. If 15 * found, returns the index where it was found. If not found, 16 * returns npos. 17 */
示例代码
1 void xx_find_first_of_1() 2 { 3 //size_type std::basic_string::find_first_of(const basic_string &__str, size_type __pos); 4 printf("%s():\n", __func__); 5 const string strSrc = "abcdefghijklmnopqrstuvwxyz"; 6 string strDst; 7 size_t pos = 0; 8 9 strDst= "cde"; 10 pos = strSrc.find_first_of(strDst);//default value of pos is 0 11 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 12 13 pos = strSrc.find_first_of(strDst, 2); 14 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 15 16 pos = strSrc.find_first_of(strDst, 3); 17 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 18 19 pos = strSrc.find_first_of(strDst, 4); 20 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 21 22 pos = strSrc.find_first_of(strDst, 5); 23 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 24 }
运行结果
xx_find_first_of_1(): find [cde] pos = 2 [find] abcdefghijklmnopqrstuvwxyz pos = 2 [find] abcdefghijklmnopqrstuvwxyz pos = 3 [find] abcdefghijklmnopqrstuvwxyz pos = 4 [find] abcdefghijklmnopqrstuvwxyz pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
作 者:fengbohello
个人网站:http://www.fengbohello.top/
E-mail : fengbohello@foxmail.com
欢迎转载,转载请注明作者和出处。
因作者水平有限,不免出现遗漏和错误。希望热心的同学能够帮我指出来,我会尽快修改。愿大家共同进步,阿里嘎多~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?