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

  View Code

实例代码:

复制代码
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;

  View Code

示例代码:

复制代码
 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

  View Code

   实例代码:

复制代码
 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;

  View Code

  示例代码:

复制代码
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

  View Code

示例代码:

复制代码
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

  View Code

示例代码:

复制代码
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)

  View Code

示例代码

复制代码
 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
复制代码

 



本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/4533328.html,如需转载请自行联系原作者


相关文章
|
1天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
9 0
|
1天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
8 0
|
4天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
5天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
12天前
|
Java API 索引
Java基础—笔记—String篇
本文介绍了Java中的`String`类、包的管理和API文档的使用。包用于分类管理Java程序,同包下类无需导包,不同包需导入。使用API时,可按类名搜索、查看包、介绍、构造器和方法。方法命名能暗示其功能,注意参数和返回值。`String`创建有两种方式:双引号创建(常量池,共享)和构造器`new`(每次新建对象)。此外,列举了`String`的常用方法,如`length()`、`charAt()`、`equals()`、`substring()`等。
14 0
|
28天前
|
Java
【Java】如果一个集合中类型是String如何使用拉姆达表达式 进行Bigdecimal类型计算?
【Java】如果一个集合中类型是String如何使用拉姆达表达式 进行Bigdecimal类型计算?
25 0
|
1月前
|
Java
Java String split()方法详细教程
Java String split()方法详细教程
20 0
|
1月前
|
安全 Java
Java StringBuffer 和 StringBuilder 类
Java StringBuffer 和 StringBuilder 类
16 0
|
1月前
|
存储 缓存 安全
【Java】Java中String不可变性的底层实现
【Java】Java中String不可变性的底层实现
14 0