1if (string1.find(string2) != std::string::npos) {
2 std::cout << "found!" << '\n';
3}
1// string::find
2#include <iostream> // std::cout
3#include <string> // std::string
4
5int main ()
6{
7 std::string str ("There are two needles in this haystack with needles.");
8 std::string str2 ("needle");
9
10 // different member versions of find in the same order as above:
11 std::size_t found = str.find(str2);
12 if (found!=std::string::npos)
13 std::cout << "first 'needle' found at: " << found << '\n';
14
15 found=str.find("needles are small",found+1,6);
16 if (found!=std::string::npos)
17 std::cout << "second 'needle' found at: " << found << '\n';
18
19 found=str.find("haystack");
20 if (found!=std::string::npos)
21 std::cout << "'haystack' also found at: " << found << '\n';
22
23 found=str.find('.');
24 if (found!=std::string::npos)
25 std::cout << "Period found at: " << found << '\n';
26
27 // let's replace the first needle:
28 str.replace(str.find(str2),str2.length(),"preposition");
29 std::cout << str << '\n';
30
31 return 0;
32}