可以用find()函数,具体的语法如下所示:
(1) size_type find(E c, size_type pos = npos) const;用来查找单个字符在字符串中出现的位置并返回位置基于0的索引值。
(2) size_type find(const E *s, size_type pos = npos) const;用来查找以空字符结尾的字符数组在字符串中出现的位置并返回该位置基于0索引值。
(3) size_type find(const E *s, size_type pos, size_type n = npos) const;用来查找以空字符结尾的字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
(4) size_type find(const basic_string &str, size_type pos = npos) const;用来查找字符串并且返回该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。
#include
#include
using namespace std;
void main()
{
string a="0AA00AA00";
char str[]="AA";
int pos=a.find(str,2);
cout<
#include
#include
using namespace std;
void main()
{
int pos=1;
string a="0AA00AA00";
char str[]="AA";
while(1)
{
pos=a.find(str,pos+1);
if(pos==-1)break;
cout<}
system("pause");
}
for(size_t i = 0; i != a.size(); i++)
{
if(a[i] == 'A' && a[i+1] == 'A')
cout << "a[" << i << "]" << " and "
<< "a[" << i+1 << "]" << " are AA" << endl;
}