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

本文详细介绍了C++中字符串类的各种查找方法,包括精确匹配查找、任意匹配字符查找及不匹配字符查找等,并通过实例演示了这些方法的具体应用。

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


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值