c++中字符串的查找方法是什么

   2024-10-24 5230
核心提示:在C++中,字符串的查找可以通过使用find()方法来实现。find()方法通常用于在一个字符串中查找另一个子字符串的位置。例如:#incl

在C++中,字符串的查找可以通过使用find()方法来实现。find()方法通常用于在一个字符串中查找另一个子字符串的位置。例如:

#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 << "Found at position: " << found << std::endl;    } else {        std::cout << "Not found" << std::endl;    }    return 0;}

在上面的例子中,我们首先定义了一个字符串str和一个子字符串subStr,然后使用find()方法在str中查找subStr的位置。如果找到了子字符串,则返回其位置;如果没找到,则返回std::string::npos

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号