C++string中find,rfind 与 find_first_of,find_last_of

1. size_t find (const string& str, size_t pos = 0)

说明:

说明:在字符串中搜索由其参数str指定的序列第一个匹配项。如果指定了pos,则搜索只包括位置pos处或位置之后的字符,忽略任何可能出现的包含位置之前的字符的匹配项。每当搜索多个字符时,必须匹配整个序列,这与 **find_first_of ** 不同。返回 第一个匹配项的第一个字符的位置。如果没有找到匹配项,该函数将返回string::npos。
参考:find函数:http://www.cplusplus.com/reference/string/string/find/

例子:

// 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");

  // 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);
  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;
}

输出:

在这里插入图片描述

2. size_t rfind (const string& str, size_t pos = npos)

说明:

说明:在字符串中搜索由其参数str指定的序列最后一个匹配项。如果指定了pos,则搜索只包括位置pos或位置pos之前开始的字符序列,忽略从pos之后开始的任何可能的匹配。每当搜索多个字符时,必须匹配整个序列。返回 最后一个匹配的第一个字符的位置。如果没有找到匹配项,该函数将返回string::npos。
参考:find函数:http://www.cplusplus.com/reference/string/string/rfind/

例子:

// string::rfind
#include <iostream>
#include <string>
#include <cstddef>

int main ()
{
  std::string str ("The sixth sick sheik's sixth sheep's sick.");
  std::string key ("sixth");

  std::size_t found = str.rfind(key);
  if (found!=std::string::npos)
    str.replace (found,key.length(),"seventh");

  std::cout << str << '\n';

  return 0;
}

输出:

在这里插入图片描述

3. size_t find_first_of (const string& str, size_t pos = 0)

说明:

说明:在字符串中搜索与其参数str中指定的任意字符匹配的第一个字符。如果指定了pos,则搜索只包括pos位置或位置之后的字符,忽略pos之前可能出现的任何字符。是str中的任意一个字符匹配就足够了(不是所有字符)匹配的第一个字符的位置。如果没有找到则返回string::npos。
参考:find_first_of函数:http://www.cplusplus.com/reference/string/string/find_first_of/

例子:

// string::find_first_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("Please, replace the vowels in this sentence by 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;
}

输出:

在这里插入图片描述

4.size_t find_last_of (const string& str, size_t pos = npos)

说明:

说明:在字符串中搜索与其参数str中指定的任意字符匹配的最后一个字符。如果指定了pos,则搜索只包括位置pos或位置pos之前的字符,忽略pos之后可能出现的任何字符。是str中的任意一个字符匹配就足够了(不是所有字符)。匹配的最后一个字符的位置。如果没有找到匹配项,该函数将返回string::npos。
参考: find_last_of函数:http://www.cplusplus.com/reference/string/string/find_last_of/

例子:

// string::find_last_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>         // std::size_t

void SplitFilename (const std::string& str)
{
  std::cout << "Splitting: " << str << '\n';
  std::size_t found = str.find_last_of("/\\");
  std::cout << " path: " << str.substr(0,found) << '\n';
  std::cout << " file: " << str.substr(found+1) << '\n';
}

int main ()
{
  std::string str1 ("/usr/bin/man");
  std::string str2 ("c:\\windows\\winhelp.exe");

  SplitFilename (str1);
  SplitFilename (str2);

  return 0;
}

输出:

在这里插入图片描述

注意:

find_first_of 和 find_last_of 匹配的是指定str中任意一字符,find 和 rfind 匹配的是指定str整个序列

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值