字符串处理函数 find(), find_first_of() 和 find_last_of()的使用

在leedcode上刷题遇见了一个问题,错误的使用字符串函数find_last_of(),随查阅资料整理了这个三个函数的使用。

find()函数:

语法:

  size_type find( const basic_string &str, size_type index );
  size_type find( const char *str, size_type index );
  size_type find( const char *str, size_type index, size_type length );
  size_type find( char ch, size_type index );
  • 返回str在字符串中第一次出现的位置(从index开始查找)如果没找到则返回string::npos,
  • 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
  • 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

例如,

    string s("Hallow world balabala!");
    unsigned int loc = s.find( "wo", 0 );
    if( loc != string::npos )
      cout << "Position:" << loc << endl;
    else
      cout << "Didn't find" << endl;

find_first_of()函数:

语法:

  size_type find_first_of( const basic_string &str, size_type index = 0 );
  size_type find_first_of( const char *str, size_type index = 0 );
  size_type find_first_of( const char *str, size_type index, size_type num );
  size_type find_first_of( char ch, size_type index = 0 );
  • 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。从index向后开始,如果没找到就返回string::npos
  • 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。从index向后开始,最多搜索num个字符。如果没找到就返回string::npos,
  • 查找在字符串中第一个与ch匹配的字符,返回它的位置。从index向后开始

find_last_of

语法:

  size_type find_last_of( const basic_string &str, size_type index = npos );
  size_type find_last_of( const char *str, size_type index = npos );
  size_type find_last_of( const char *str, size_type index, size_type num );
  size_type find_last_of( char ch, size_type index = npos );
  • 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。从index开始向前搜索,如果没找到就返回string::npos
  • 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。从index开始向前搜索,最多搜索num个字符。如果没找到就返回string::npos
  • 在字符串中查找最后一个与ch匹配的字符,返回它的位置。从index开始向前搜索。如果没找到就返回string::npos

   在使用过程,当不设置搜索位置时,find()、find_first_of()默认从字符串0号位置搜索,find_last_of()默认从字符串最后一个字符位置查找。

例如:

我们从字符串中查找空字符出现的位置:

// main() 函数中:

    string s("Hellow world abc!");

    unsigned int loc1 = s.find(' ',0);
    unsigned int loc2 = s.find_first_of(' ',3);
    unsigned int loc3 = s.find_last_of(' ',s.size()-1);

    if (loc1 != string::npos && loc2 != string::npos && loc3 != string::npos)
    {
        cout << "position of ' ':" << loc1 << endl;
        cout << "First position of ' ':" << loc2 << endl;
        cout << "Last position of ' ':" << loc3 << endl;
    }
    else
    {
        cout << "Didn't find some position:" << endl;
        cout << "position" << loc1 << loc2 << loc3 << endl;
    }    


// 结果:

position of ' ':6
First position of ' ':6
Last position of ' ':12

//说明:

三个函数分别从0号位置,3号位置,和字符串最后一个位置开始查找,前两个从前往后查找,找到第一个空格,就返回其位置,最后一个从后往前查找,也是找到第一空格,就返回其位置。

 参考博客:

https://www.cnblogs.com/aminxu/p/4686320.html#operators

https://blog.csdn.net/zhangxiao93/article/details/54381613/

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值