c++: string中 find, rfind, find_frist_of, find_laste_of 与 substr之间的操作

文章详细介绍了C++标准库中的std::string类提供的find(),rfind(),find_first_of(),find_last_of()和substr()函数,用于在字符串中执行搜索、定位和子串提取操作。
摘要由CSDN通过智能技术生成

在 C++ 的 std::string 类中,有几个成员函数可以用于在字符串中执行搜索和子字符串提取操作。以下是这些函数的简要说明:

  1. find(): 查找子字符串的第一个出现位置。

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

    这个函数返回子字符串 str 或 C 字符串 s 第一次出现的位置(索引)。可以指定搜索的起始位置 pos。如果找不到子字符串,返回 string::npos

  2. rfind(): 反向查找子字符串的最后一个出现位置。

    size_t rfind(const string& str, size_t pos = npos) const;
    size_t rfind(const char* s, size_t pos = npos) const;
    

    这个函数返回子字符串 str 或 C 字符串 s 最后一次出现的位置(索引)。可以指定搜索的起始位置 pos,默认情况下从字符串的末尾开始搜索。如果找不到子字符串,返回 string::npos

  3. find_first_of(): 查找给定字符集合中任意字符第一次出现的位置。

    size_t find_first_of(const string& str, size_t pos = 0) const;
    size_t find_first_of(const char* s, size_t pos = 0) const;
    

    这个函数返回在子字符串 str 或 C 字符串 s 中任意字符的第一次出现的位置(索引)。可以指定搜索的起始位置 pos。如果找不到字符,返回 string::npos

  4. find_last_of(): 反向查找给定字符集合中任意字符最后一次出现的位置。

    size_t find_last_of(const string& str, size_t pos = npos) const;
    size_t find_last_of(const char* s, size_t pos = npos) const;
    

    这个函数返回在子字符串 str 或 C 字符串 s 中任意字符的最后一次出现的位置(索引)。可以指定搜索的起始位置 pos,默认情况下从字符串的末尾开始搜索。如果找不到字符,返回 string::npos

  5. substr(): 提取子字符串。

    string substr(size_t pos = 0, size_t len = npos) const;
    

    这个函数返回从位置 pos 开始,长度为 len 的子字符串副本。如果省略 len,则返回从 pos 开始的剩余部分。

具体示例:

#include <iostream>
#include <string>

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

    // 使用 find() 查找子字符串的第一个出现位置
    size_t pos = str.find("World");
    if (pos != std::string::npos) {
        std::cout << "'World' found at position " << pos << std::endl;
    } else {
        std::cout << "'World' not found" << std::endl;
    }

    // 使用 rfind() 反向查找子字符串的最后一个出现位置
    size_t pos_reverse = str.rfind("o");
    if (pos_reverse != std::string::npos) {
        std::cout << "'o' found at position " << pos_reverse << std::endl;
    } else {
        std::cout << "'o' not found" << std::endl;
    }

    // 使用 find_first_of() 查找给定字符集合中任意字符的第一个出现位置
    size_t pos_first_of = str.find_first_of("eio");
    if (pos_first_of != std::string::npos) {
        std::cout << "Any of 'eio' found at position " << pos_first_of << std::endl;
    } else {
        std::cout << "Any of 'eio' not found" << std::endl;
    }

    // 使用 find_last_of() 反向查找给定字符集合中任意字符的最后一个出现位置
    size_t pos_last_of = str.find_last_of("rlod");
    if (pos_last_of != std::string::npos) {
        std::cout << "Any of 'rlod' found at position " << pos_last_of << std::endl;
    } else {
        std::cout << "Any of 'rlod' not found" << std::endl;
    }

    // 使用 substr() 提取子字符串
    std::string substr = str.substr(7, 5);
    std::cout << "Substring: " << substr << std::endl;

    return 0;
}

输出结果:

'World' found at position 7
'o' found at position 8
Any of 'eio' found at position 1
Any of 'rlod' found at position 13
Substring: World

以上示例演示了如何使用这些函数在字符串中查找子字符串并提取子字符串的各种操作。

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值