string类型的查找

sting s;

 

记住典型的查找操作:

s.find(args)                   

s.find_fist_of(args)

s.find_fist_not_of(args)

 

还有3个逆序的查找操作,

s.rfind(args)

s.find_last_of(args)

s.find_last_not_of(args)

 

 

其中args可以为下面4个版本之一:

c,pos

s2,pos

cp,pos

cp,pos,n

 

 

例子:

/******************************************************************************

 * str.find(args)是完全匹配args中的字符串;

 * 注意:args是参数表(含要查找的字符或字符串参数),不是字符串!

 ********************************************************************************/



#include <iostream>

#include <string>



using namespace std;

int main()

{

	string str = "helloWorLD";

	string::size_type pos1 = str.find("Wor");

	cout << pos1 << endl;



	string::size_type pos2 = str.find("woa");

	if (pos2 == string::npos) {

		cout << "Do not find" << endl;

	} else {

		cout << pos2 << endl;;

	}

	return 0;

}

 

输出:

5

Do not find

 

 

/******************************************************************************

 * str.find_first_of(args)只要在str中找到args中的字符串参数中的任意字符就返回;

 * 注意:args是参数表(含要查找的字符或字符串参数),不是字符串!

 ********************************************************************************/



#include <iostream>

#include <string>



using namespace std;



int main()

{

	string str = "he2l3l5oWo6rLD";

	string::size_type pos = str.find_first_of("0123456789");

	cout << pos << endl;

	return 0;

}

 

输出:

2

你可能感兴趣的:(String类)