母串中寻找字串

find顺搜索
rfind逆搜索
string::npos表示母串中不含字串
index表示位置

#include
#include
#include

using namespace std;

void main()

{
    string text = "had had sdsdsdhad";
    string word = "had";

    int count = 0;
    for (int index = 0; (index = text.find(word, index)) != string::npos; index = word.length() + index, count++);
    
    cout << count;

    system("pause");
        return;
}

你可能感兴趣的:(母串中寻找字串)