string----------查找
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;
else
cout<<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;
else
cout<<"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;
}

浙公网安备 33010602011771号