首页 > 其他分享 >string的find()与npos

string的find()与npos

时间:2024-09-23 17:27:00浏览次数:1  
标签:std string 查找 npos find size

在 C++ 中,std::string::find() 是一个用于在字符串中查找子字符串或字符的成员函数。查找成功时返回匹配的索引位置,查找失败时返回 std::string::npos,表示未找到。

std::string::find() 函数原型

std::size_t find(const std::string& str, std::size_t pos = 0) const noexcept;
std::size_t find(const char* s, std::size_t pos = 0) const;
std::size_t find(char c, std::size_t pos = 0) const noexcept;

参数说明

  1. str/s:要查找的子字符串或字符。
  2. pos(可选):从哪个位置开始查找,默认为 0,即从字符串的开始位置查找。
  3. 返回值:查找成功时返回第一个匹配字符的索引,查找失败时返回 std::string::npos

std::string::npos

std::string::npos 是一个常量,表示查找操作失败或子字符串不存在时的返回值。具体定义为 std::string::npos = -1,它实际上是一个 std::size_t 类型的最大值。

示例

以下是一些简单的示例,演示如何使用 std::string::find()std::string::npos

查找子字符串

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    // 查找子字符串 "World"
    std::size_t found = str.find("World");
    if (found != std::string::npos) {
        std::cout << "Found 'World' at index: " << found << std::endl; // 输出: Found 'World' at index: 7
    } else {
        std::cout << "'World' not found." << std::endl;
    }

    // 查找不存在的子字符串 "Earth"
    found = str.find("Earth");
    if (found == std::string::npos) {
        std::cout << "'Earth' not found." << std::endl; // 输出: 'Earth' not found.
    }

    return 0;
}

查找字符

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    // 查找字符 'o'
    std::size_t found = str.find('o');
    if (found != std::string::npos) {
        std::cout << "Found 'o' at index: " << found << std::endl; // 输出: Found 'o' at index: 4
    }

    return 0;
}

从指定位置开始查找

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    // 从索引 5 开始查找字符 'o'
    std::size_t found = str.find('o', 5);
    if (found != std::string::npos) {
        std::cout << "Found 'o' at index: " << found << std::endl; // 输出: Found 'o' at index: 8
    }

    return 0;
}

总结

  • std::string::find():用于查找字符串或字符。返回子字符串第一次出现的索引,或者返回 std::string::npos 表示未找到。
  • std::string::npos:表示查找操作失败时的返回值(通常为最大值 -1 表示无效位置)。

标签:std,string,查找,npos,find,size
From: https://www.cnblogs.com/niumachen/p/18427437

相关文章

  • 什么情况下需要 new Date().toISOString()?,是否会受时区的影响吗?
    newDate().toISOString()是JavaScript中用于获取当前日期和时间的ISO8601格式字符串的方法。格式为YYYY-MM-DDTHH:MM:SS.sssZ。这种格式的字符串在很多场景中都非常有用,特别是在需要标准化日期和时间表示的情况下。以下是一些常见的使用场景:1.API通信在与后端API通......
  • `std::string_view`(c++17) 和 `std::stringstream` 使用区别·
    std::string_view和std::stringstream都是C++中处理字符串的工具,但它们的设计目标和使用场景非常不同。我们可以通过几方面进行对比。1.设计目的和核心功能std::string_view:设计用于只读访问字符串或字符序列。是一个轻量级的字符串视图,不会持有字符串的数据,仅仅是对......
  • C++ string
    在C++中,std::string类是处理字符串的主要工具。它提供了丰富的功能和方法来简化字符串操作,使得开发者可以更加方便地进行文本数据的处理。构造函数std::string类有多种构造函数供用户选择:使用C风格字符串初始化:string(constchar*s);使用指定字符重复初始化:string(size_......
  • 为何我安装完提示这个报错?:Array and string offset access syntax with curly braces
    错误信息 Arrayandstringoffsetaccesssyntaxwithcurlybracesisdeprecated 表明你在使用的PHP版本较高,而你的程序代码中使用了一些在较新版本中已弃用的语法。具体来说,这是PHP7.4及以上版本对数组和字符串偏移量访问语法 {} 的弃用警告。解决方案1.降低PHP......
  • 【C++驾轻就熟】string类以及string类的模拟实现
    目录一、为什么学习string类?二、标准库中的string类 2.1string类(了解)2.2string类的常用接口说明 1.string类对象的常见构造 2.string类对象的容量操作3.string类对象的访问及遍历操作 4.string类对象的修改操作5.string类非成员函数 三、 string类的......
  • SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xF0\x9F...' for
    错误信息 SQLSTATE[HY000]:Generalerror:1366Incorrectstringvalue:'\xF0\x9F...'forcolumn'content'atrow1 表示在插入数据时,content 字段的内容包含了一些不支持的特殊字符(如表情符号),导致插入失败。这是因为MySQL的 utf8 编码不支持某些四字节的Unicode字......
  • SQLSTATE[HY000]: General error: 1366 Incorrect string value: 'xF0xA7x92xADxEFxBC
    错误信息 Generalerror:1366Incorrectstringvalue:'\xF0\x9F\x98'forcolumn'content' 表示在插入数据时,content 字段的内容包含了一些不支持的特殊字符(如表情符号),导致插入失败。这是因为MySQL的 utf8 编码不支持某些四字节的Unicode字符,而 utf8mb4 编码则支持......
  • BZOJ 2555 = P5212 SubString 题解
    Statement给你一个字符串 \(\text{init}\),要求你支持两个操作:在当前字符串的后面插入一个字符串;询问字符串 \(s\) 在当前字符串中出现了几次?(作为连续子串)你必须在线支持这些操作。Solutionextend中link[cur]=q,相当于link,链加新建copy那里,相当于link,cut,链加询......
  • string类(上)(解析各种成员函数)
    文章目录1.为什么要学习string类2.标准库中的string类2.1构造函数2.2成员函数2.3与内存相关的成员函数1.`capacity()`2.`resize()`3.`reserve()`2.4其他函数的简单示例4.`insert()`和`assign()`5.`at()`和`operator[]`6.`erase()`2.5string迭代器2.7示例:......
  • shell中$后加引号有什么用($"string"和$'string')
    (1).如果没有特殊定制bash环境或有特殊需求,$"string"和"string"是完全等价的,使用$""只是为了保证本地化。以下是manbash关于$""的解释:Adouble-quotedstringprecededbyadollarsign($"string")willcausethestringtobetranslatedaccordi......