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}
1const char* c = "Word";
2string str = "WhereIsMyWordThatINeed";
3cout << "the word is at index " << str.find(c);
4//this will print "the word is at index 9"
1#include <iostream>
2#include <string>
3#include <algorithm>
4#include <functional>
5
6int main()
7{
8 std::string in = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
9 " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
10 std::string needle = "pisci";
11 auto it = std::search(in.begin(), in.end(),
12 std::boyer_moore_searcher(
13 needle.begin(), needle.end()));
14 if(it != in.end())
15 std::cout << "The string " << needle << " found at offset "
16 << it - in.begin() << '\n';
17 else
18 std::cout << "The string " << needle << " not found\n";
19}