Answers for "c++ contains substring"

C++
4

c++ check if string contains substring

string str ("There are two needles in this haystack.");
string str2 ("needle");

if (str.find(str2) != string::npos) {
//.. found.
}
Posted by: Guest on December-23-2020
0

c++ check substring

if (s1.find(s2) != std::string::npos) {
    std::cout << "found!" << 'n';
}
Posted by: Guest on July-07-2020
-1

c++ find string in string

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>

int main()
{
    std::string in = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
                     " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
    std::string needle = "pisci";
    auto it = std::search(in.begin(), in.end(),
                   std::boyer_moore_searcher(
                       needle.begin(), needle.end()));
    if(it != in.end())
        std::cout << "The string " << needle << " found at offset "
                  << it - in.begin() << 'n';
    else
        std::cout << "The string " << needle << " not foundn";
}
Posted by: Guest on August-21-2020

Browse Popular Code Answers by Language