Sunday, January 6, 2019

Check if a string contains a string in C++



I have a variable of type std::string. I want to check if it contains a certain std::string. How would I do that?



Is there a function that returns true if the string is found, and false if it isn't?


Answer



Use std::string::find as follows:




if (s1.find(s2) != std::string::npos) {
std::cout << "found!" << '\n';
}


Note: "found!" will be printed if s2 is a substring of s1, both s1 and s2 are of type std::string.


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...