当前位置: 首页 > news >正文

【C++】string类(二)相关接口介绍及其使用

文章目录

  • 一、string相关接口
    • 1、append
    • 2、push_back
    • 3、+=
    • 4、insert
    • 5、erase
    • 6、replace
    • 7、find
      • 练习
      • (1)方法1:使用find、replace
      • (2)方法2:遍历替换
    • 8、rfind
    • 9、find_first_of
    • 10、find_last_of
    • 11、find_first_not_of
    • 12、find_last_not_of
    • 13、substr
  • 二、谢谢观看!

上一篇文章链接:string(一)
(该篇为(一)的续作)

一、string相关接口


第一个接口就不用过多介绍了,对该字符串进行追加。

1、append

在这里插入图片描述

void test04()
{string s("hello world");s.append("aaa");cout << s << endl;
}

在这里插入图片描述

2、push_back

对字符串尾插一个字符
在这里插入图片描述

void test05()
{string s("hello world");s.push_back('A');cout << s << endl;
}

在这里插入图片描述

3、+=

+= 也是用来拼接字符/字符串的,相较以上两种接口,+=更常用。

void test06()
{string s("hello world");s += 'a';s += "bbb";cout << s << endl;
}

在这里插入图片描述

4、insert

插入
在这里插入图片描述
最常用的是第一个,在pos位置插入字符串。

void test07()
{string s("hello world");s.insert(0, "w");cout << s << endl;
}

在这里插入图片描述

5、erase

删除元素
在这里插入图片描述

void test08()
{string s("hello world");s.erase(0, 1);//在第0个位置删除1个元素cout << s << endl;
}

在这里插入图片描述

void test08()
{string s("hello world");//头删s.erase(0, 1);//在第0个位置删除1个元素//尾删// s.erase(s.size() - 1, 1);s.erase(--s.end());//迭代器cout << s << endl;
}

在这里插入图片描述

6、replace

替换
在这里插入图片描述

void test09()
{string s("hello world");s.replace(0, 2, "a");//从第0个位置开始的2个字符替换为"a"cout << s << endl;
}

在这里插入图片描述

7、find

查找子字符串str在该字符串中的位置,查找成功返回该子串的首字符位置,失败返回npos
在这里插入图片描述

void test11()
{string s("hello world");cout << s.find(" ");//输出结果为:5
}

练习

练习:
将字符串中的空格全部替换为“%%”

(1)方法1:使用find、replace

void test10()
{string s("hello world  aaa bbb ccc  f");int pos = s.find(' ');while (pos != string::npos){s.replace(pos, 1, "%%");pos = s.find(' ', pos + 2);}cout << s << endl;
}

在这里插入图片描述

(2)方法2:遍历替换

void test10()
{string s("hello world  aaa bbb ccc  f");string tmp;for (auto ch : s){if (ch == ' ')tmp += "%%";elsetmp += ch;}s = tmp;cout << s << endl;
}

在这里插入图片描述

8、rfind

和find作用相同,只不过是倒着查找。
r

void test11()
{string s("hello world ddd");cout << s.rfind(" ");//输出结果为:11
}

9、find_first_of

与字符串str中任一字符进行匹配。
在这里插入图片描述

例如:将字符串s中含有a/b/c/d的字符全部修改为*

void test12()
{string s("hello world  abfg cdmn");int pos = s.find_first_of("abcd");while (pos != string::npos){s.replace(pos, 1, "*");pos = s.find_first_of("abcd", pos + 1);}cout << s << endl;
}

在这里插入图片描述

10、find_last_of

与字符串str中任一字符进行匹配,但是倒着进行查找匹配。
在这里插入图片描述

11、find_first_not_of

排除,查找字符串中非str中任一字符的下标。
在这里插入图片描述

例如:将字符串s中非a/b/c/d的字符全部修改为*

void test12()
{string s("hello world  abfg cdmn");int pos = s.find_first_not_of("abcd");while (pos != string::npos){s.replace(pos, 1, "*");pos = s.find_first_not_of("abcd", pos + 1);}cout << s << endl;
}

在这里插入图片描述

12、find_last_not_of

查找字符串中非str中任一字符的下标,但是倒着进行查找匹配。
在这里插入图片描述

13、substr

拷贝该字符串中第pos位置,长度为len的子串。
在这里插入图片描述

void test13()
{string s("hello world");cout << s.substr(6) << endl;
}

在这里插入图片描述

二、谢谢观看!

http://www.dtcms.com/a/268427.html

相关文章:

  • 植物大战僵尸杂交重制版1.0,经典焕新,重燃策略塔防之火
  • Altium Designer使用入门(非精通)教程 第三章(PCB绘制)
  • 前端开发深度剖析:核心痛点、隐藏陷阱与系统解决方案
  • 【MySQL进阶】MySQL架构
  • 【HarmonyOS】鸿蒙应用开发Text控件常见错误
  • AI+Web3:从自动化工具到自主经济体的范式革命
  • 爬虫-协议基础
  • 1865.找出和为指定值得下标对
  • Java笔记-下
  • MyBatis-Plus分页拦截器原理深度解析
  • new与malloc[c++面试系列]
  • GCC/G++编译器详解:从编译原理到动静态链接
  • 2025 JuniorCryptCTF re 部分wp
  • 【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
  • 【Docker基础】Docker数据卷管理:docker volume rm与prune命令对比
  • 计算机网络实验——配置ACL
  • vue3 当前页面方法暴露
  • 「Java题库」基础程序设计(理论+操作)
  • Excel 日期计算与最小日期选择(附示例下载)
  • DAY 49
  • monorepo + Turborepo --- 开发应用程序
  • Go语言实现双Token登录的思路与实现
  • 微服务基础:Spring Cloud Alibaba 组件有哪些?
  • 随机森林算法详解:Bagging思想的代表算法
  • 自存bro code java course 笔记(2025 及 2020)
  • 【Linux网络编程】Socket - UDP
  • CppCon 2018 学习:What do you mean “thread-safe“
  • Linux操作系统之文件(五):文件系统(下)
  • 数据库|达梦DM数据库安装步骤
  • 谷歌浏览器安全输入控件-allWebSafeInput控件