Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c++ string.find

作者:倾尽尘光暖流年   发布日期:2025-10-13   浏览:11

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, world!";
    std::string substr = "world";

    // 使用 string.find 查找子字符串的位置
    size_t found = str.find(substr);

    if (found != std::string::npos) {
        std::cout << "Substring found at position: " << found << std::endl;
    } else {
        std::cout << "Substring not found" << std::endl;
    }

    return 0;
}

解释说明:

  • std::string::find 是 C++ 标准库中 std::string 类的一个成员函数,用于查找指定子字符串在当前字符串中的位置。
  • 如果找到了子字符串,find 函数会返回该子字符串首次出现的位置(从 0 开始的索引)。
  • 如果没有找到子字符串,则返回 std::string::npos,这是一个特殊的常量,表示未找到。
  • 在示例代码中,我们查找子字符串 "world" 在字符串 "Hello, world!" 中的位置,并根据查找结果输出相应的信息。

上一篇:c++ 开发

下一篇:c++ setprecision

大家都在看

c++闭包

c++单引号和双引号的区别

c++ 注释

c++如何判断素数

c++格式化字符串

c++ orm框架

队列c++

c++freopen怎么用

c++ 获取系统时间

c++进制转换函数

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站