find substring in string c++ stl code example
Example 1: find all occurrences of a substring in a string c++
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s("hello hello");
int count = 0;
size_t nPos = s.find("hello", 0);
while(nPos != string::npos)
{
count++;
nPos = s.find("hello", nPos + 1);
}
cout << count;
};
Example 2: c++ string contains
if (string1.find(string2) != std::string::npos) {
std::cout << "found!" << '\n';
}
Example 3: find in string c++
size_t find (const string& str, size_t pos = 0) const;