0

I've been trying to get a specific string from another string in c++, and I was wondering if there was an "includes()" method like there is in javascript, which returns true if the string parameter is inside the other specified string. If not, what would be the closest thing to it?

avisk
  • 330
  • 2
  • 6
  • 16
  • 3
    You probably want [`std::string::find`](https://en.cppreference.com/w/cpp/string/basic_string/find). – George Nov 20 '19 at 01:45
  • What does your C++ book say, as far as available `std::string` methods go? Do any one of the many `std::string` methods look like something that you're looking for? Which C++ `std::string` methods you know, already? – Sam Varshavchik Nov 20 '19 at 01:46
  • 1
    Tip: When you phrase your question that way, you restrict your audience to people who know both JavaScript and C++. If you took the time to explain the functionality of "includes()", you would expand your audience. (I don't know by how much, though.) – JaMiT Nov 20 '19 at 01:48

1 Answers1

0

Duplicate of this question.

The std::string::find function will return the position of the "substring" inside the string. If the substring is not found, it returns std::string::npos.

An includes() function may look like:

bool includes(const std::string &str, const std::string &subStr) {
    return (str.find(subStr) != std::string::npos);
}
lucie
  • 895
  • 5
  • 19