c++中string.find函数的用法是什么
在C++中,string.find()函数用于在字符串中搜索指定的子字符串,并返回第一次出现的位置索引。如果找到子字符串,则返回第一次出现的位置索引;如果未找到,则返回string::npos。
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
std::string subStr = "world";
size_t found = str.find(subStr);
if (found != std::string::npos) {
std::cout << "Substring found at index: " << found << std::endl;
} else {
std::cout << "Substring not found" << std::endl;
}
return 0;
}
在上面的示例中,我们首先定义了一个字符串str和一个子字符串subStr。然后,我们使用string.find()函数在字符串str中搜索子字符串subStr,并将结果存储在变量found中。最后,我们检查found的值,如果不等于string::npos,则说明找到了子字符串,打印出子字符串在字符串中的位置索引;否则,打印出子字符串未找到的信息。
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:oracle怎么查看当前连接的用户 下一篇:mysql如何查看数据库信息
博豪信息



发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。