关于string类的find()一系列函数

本文详细介绍了C++中字符串的查找函数,包括find、find_first_of、find_first_not_of、find_last_of和find_last_not_of等函数的使用方法及示例。这些函数帮助开发者在字符串中查找指定字符或子串的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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


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

find_first_of()函数:
查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::npos,
查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始。


find_first_not_of
语法:

  size_type find_first_not_of( const basic_string &str, size_type index = 0 );    
  size_type find_first_not_of( const char *str, size_type index = 0 );    
  size_type find_first_not_of( const char *str, size_type index, size_type num );    
  size_type find_first_not_of( char ch, size_type index = 0 );  

find_first_not_of()函数:
在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回string::nops
在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

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

find_last_of()函数:
在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops
在字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

find_last_not_of
语法:

  size_type find_last_not_of( const basic_string &str, size_type index = npos );    
  size_type find_last_not_of( const char *str, size_type index = npos);    
  size_type find_last_not_of( const char *str, size_type index, size_type num );    
  size_type find_last_not_of( char ch, size_type index = npos );  

find_last_not_of()函数:
在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符如果没找到就返回string::nops
在字符串中查找最后一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

这些函数都是异曲同工。 对于单字符的函数来说。是查找字符在str里第一次出现的位置。

对于字符串或者数组来说,是查找字符或数字任意字符在str第一次匹配的位置。 这些函数索引都是从0开始数。

 string str1("heartbeat");
        string str2("abcde");
        int npos=0;
        npos=str1.find_first_of(str2,0);                
        cout<<str2<<" in "<<str1<<" position is "<<npos<<endl;    // 1

 npos=str1.find_first_of('e',2);    
        cout<<'e'<<" in "<<str1<<" position is "<<npos<<endl;        //6

 npos=str1.find_first_of(str2,2);               
        cout<<str2<<" in "<<str1<<" position is "<<npos<<endl;      //2

 char charValue[]={'a','e','i','o','u'};
        npos=str1.find_first_of(charValue,4,2);    //最后的参数 是限定数组中参与查找的个数
        cout<<"aeiou"<<" in "<<str1<<" position is "<<npos<<endl;     //6


  其实这些函数只要记住,索引从零开始。就会用了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值