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

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

string中 find()的应用  (rfind() 类似,只是从反向查找)

原型如下:
(1)size_t find (const string& str, size_t pos = 0) const;  //查找对象-- string类对象
(2)size_t find (const char* s, size_t pos = 0) const; //查找对象-- 字符串
(3)size_t find (const char* s, size_t pos, size_t n) const;  //查找对象-- 字符串的前n个字符
(4)size_t find (char c, size_t pos = 0) const;  //查找对象-- 字符
结果:找到 -- 返回 第一个字符的索引
     没找到--返回   string::npos
 
示例:
  1. #include <iostream>       // std::cout  
  2. #include <string>         // std::string  
  3.   
  4. int main ()  
  5. {  
  6.   std::string str ("There are two needles in this haystack with needles.");  
  7.   std::string str2 ("needle");  
  8.   
  9.   // different member versions of find in the same order as above:  
  10.   std::size_t found = str.find(str2);  
  11.   if (found!=std::string::npos)  
  12.     std::cout << "first 'needle' found at: " << found << '\n';  
  13.   
  14.   found=str.find("needles are small",found+1,6);  
  15.   if (found!=std::string::npos)  
  16.     std::cout << "second 'needle' found at: " << found << '\n';  
  17.   
  18.   found=str.find("haystack");  
  19.   if (found!=std::string::npos)  
  20.     std::cout << "'haystack' also found at: " << found << '\n';  
  21.   
  22.   found=str.find('.');  
  23.   if (found!=std::string::npos)  
  24.     std::cout << "Period found at: " << found << '\n';  
  25.   
  26.   // let's replace the first needle:   
  27.   str.replace(str.find(str2),str2.length(),"preposition");  //replace 用法  
  28.   std::cout << str << '\n';  
  29.   
  30.   return 0;  
  31. }  
#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");  //replace 用法
  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
 
 
其他还有  find_first_of(), find_last_of(), find_first_not_of(), find_last_not_of()
作用是查找   字符串中 任一个字符 满足的查找条件
string snake1("cobra");
int where = snake1.find_first_of("hark");
返回3  因为 "hark"中 各一个字符 在 snake1--cobra 中第一次出现的是  字符'r'(3为 cobra 中'r'的索引)
同理:
int where = snake1.find_last_of("hark");
返回4  因为 "hark"中 各一个字符 在 snake1--cobra 中最后一次出现的是  字符'a'(3为 cobra 中'r'的索引)
 

rfind全名reversefind

与find相反,

  1. size_type rfind( const basic_string &str, size_type index );  
  2. size_type rfind( const char *str, size_type index );  
  3. size_type rfind( const char *str, size_type index, size_type num );  
  4. size_type rfind( char ch, size_type index );  
  size_type rfind( const basic_string &str, size_type index );
  size_type rfind( const char *str, size_type index );
  size_type rfind( const char *str, size_type index, size_type num );
  size_type rfind( char ch, size_type index );

 

rfind()函数:

  • 返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos
  • 返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回string::npos
  • 返回最后一个与ch匹配的字符,从index开始查找。如果没找到就返回string::npos

这里要注意与find不同的地方,rfind是从左往右数index个位置,这里作为基准index,然后从这里开始,从右往左数到第一次出现目标的位置。

find是在基准index从左往右数

例子

  1. #include <cstring>   
  2. #include <iostream>   
  3.   
  4. int main()  
  5. {  
  6.     int loc;  
  7.     std::string s = "My cat's breath smells like cat food.";  
  8.   
  9.     loc = s.rfind( "breath", 8 );  
  10.     std::cout << "The word breath is at index " << loc << std::endl;  
  11.     //-1   
  12.     loc = s.rfind( "breath", 9 );  
  13.     std::cout << "The word breath is at index " << loc << std::endl;  
  14.     //9   
  15.   
  16.     loc = s.rfind( "breath", 20 );  
  17.     std::cout << "The word breath is at index " << loc << std::endl;  
  18.     //9   
  19.   
  20.     std::string ss = "Hi Bill, I'm ill, so please pay the bill";  
  21.     loc = ss.rfind("ill");  
  22.     std::cout << "The word breath is at index " << loc << std::endl;  
  23.     //37   
  24.     loc = ss.rfind("ill",20);  
  25.     std::cout << "The word breath is at index " << loc << std::endl;  
  26.     //13   
  27. }  
#include <cstring>
#include <iostream>

int main()
{
	int loc;
    std::string s = "My cat's breath smells like cat food.";

	loc = s.rfind( "breath", 8 );
	std::cout << "The word breath is at index " << loc << std::endl;
	//-1
    loc = s.rfind( "breath", 9 );
	std::cout << "The word breath is at index " << loc << std::endl;
	//9

    loc = s.rfind( "breath", 20 );
	std::cout << "The word breath is at index " << loc << std::endl;
	//9

	std::string ss = "Hi Bill, I'm ill, so please pay the bill";
	loc = ss.rfind("ill");
	std::cout << "The word breath is at index " << loc << std::endl;
	//37
	loc = ss.rfind("ill",20);
	std::cout << "The word breath is at index " << loc << std::endl;
	//13
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值