string里find()函数,判断字符串是否包含某个子串

xiaoxiao2021-02-28  18

使用 string 的 find 成员函数。 #include <iostream> #include <string> using namespace std; int main() { string str = "afdsdfs_hello_sdfas#@!"; string str1 = "hello"; string::size_type idx = str.find( str1 ); if ( idx != string::npos ) { cout << "字符串含有“<< str1 << "\n"; } else { cout << "字符串没有" << str1 << "\n"; } } 解析:string::npos是个返回值
转载请注明原文地址: https://www.6miu.com/read-1900091.html

最新回复(0)