c++ std::string contains实现
c++ std::string contains实现
#include <iostream> #include <string> bool contains(const std::string& str, const std::string& substr) { return str.find(substr) != std::string::npos; } int main() { std::string mainStr = "Hello, World!"; std::string toFind = "World"; if (contains(mainStr, toFind)) { std::cout << "The string contains '" << toFind << "'." << std::endl; } else { std::cout << "The string does not contain '" << toFind << "'." << std::endl; } return 0; }
欢迎讨论,相互学习。
cdtxw@foxmail.com