string----------查找

来源:互联网 发布:磁头坏了 数据恢复 编辑:程序博客网 时间:2024/05/17 04:37

s.find(args)           严格匹配查找

s.rfind(args)          严格匹配查找

s.find_first_of(args, pos)       查找任意匹配字符,从pos开始找,        

s.find_last_of(args,  pos)

s.find_first_not_of(args)            查找任意不匹配字符

s.find_last_not_of(args)


#include <iostream>#include <string>using namespace std;int main( int argc, char** argv ){string name("AnnaBelle");name.find("nna");string::size_type pos1 = name.find("Belll");if( string::npos == pos1 )cout<<"not find"<<endl;elsecout<<pos1<<endl;name = "r2d%4Cd5";string num("0123456789");string::size_type pos = name.find_first_of(num);if( string::npos ==  pos )cout<<"not find"<<endl;elsecout<<"find pos:"<<pos<<endl;pos = 0;while( string::npos != (pos = name.find_first_of(num, pos)) ){cout<<"find pos:"<<pos<<endl;pos++;}pos = name.length();cout<<"nameLen:"<<name.length()<<" "<<name.size()<<endl;while( string::npos != (pos = name.find_last_of(num, pos)) ){cout<<"last_of:"<<name[pos]<<endl;--pos;}pos = 0;while( string::npos != (pos = name.find_first_not_of(num, pos)) ){cout<<name[pos]<<endl;++pos;}string letters("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");pos = 0;while( string::npos != (pos = name.find_first_of(letters, pos)) ){cout<<name[pos]<<endl;++pos;}pos = 0;while( string::npos != (pos = name.find_first_not_of(letters, pos)) ){cout<<name[pos]<<endl;++pos;}string strRiver("Mississippi");string::size_type FirstPos = strRiver.find("is");cout<<"firstPos:"<<FirstPos<<endl;string::size_type LastPos = strRiver.rfind("is");cout<<"LastPos:"<<LastPos<<endl;return 0;}


0 0
原创粉丝点击