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

C++ string数据查找、string数据替换、string子串获取

string查找示例见下,代码见下,以及对应运行结果见下:

#include<iostream>using namespace std;int main() {// 1string s1 = "hellooooworld";cout << s1.find("oooo") << endl;// 2cout << (int)s1.find("oooo", 8) << endl;cout << s1.find("oooo", 4) << endl;// 3cout << s1.find('o') << endl;cout << s1.find('o', s1.find('o') + 1) << endl;// 4 从右向左查找cout << s1.rfind("oo") << endl; }

运行结果见下:

string数据替换,代码见下:

#include<iostream>using namespace std;int main() {// 1 string s1 = "hello woooorld";s1.replace(7, 5, "or"); // 代表从第七个元素开始,取五个字符,替换成对应字符cout << s1 << endl;// 2s1 = "hello woooorld";s1.replace(s1.begin()+7, s1.begin()+12, "or"); // 这个区间是左闭右开的,所以要到12cout << s1 << endl;// 3s1 = "hello woooorld";s1.replace(s1.begin() + 7, s1.begin() + 12, "or123edafsgsrg", 2); cout << s1 << endl;}

运行结果见下

string子串获取,代码见下:

#include<iostream>using namespace std;int main() {// 1 string s1 = "hello woooorld";string sub_s1 = s1.substr(7, 4);cout << sub_s1 << endl;// 2string s2 = "xiaoxiaode&&yikeshu";int pos = s2.find("&&");string s3 = s2.substr(0, pos);cout << s3 << endl;}

运行结果见下

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

相关文章:

  • MySQL 数据库:创建新数据库和数据表全攻略
  • 在Ubuntu服务器上部署Label Studio
  • 蓝桥杯13届国B 出差
  • 软件产品线上验收测试有什么目的?又有哪些测试流程?
  • Java接口性能优化:零成本实现数据库状态到中文的极致转换
  • 【图像处理基石】遥感图像分析入门
  • 硬件自动化测试平台搭建(硅后验证,非EDA)day1 大概了解
  • 【报错解决】服务器重启后vscode远程连接失败
  • ‌Element UI 双击事件(@cell-dblclick 与 @row-dblclick)
  • 使用java通过modbus读取前端设备数据
  • AI Agent开发第65课-DIFY和企业现有系统结合实现高可配置的智能零售AI Agent(下)
  • 【Python爬虫 !!!!!!政府招投标数据爬虫项目--医疗实例项目文档(提供源码!!!)!!!学会Python爬虫轻松赚外快】
  • 现代化QML组件开发教程
  • 支付宝小程序开发指南
  • 在Babylon.js中实现完美截图:包含Canvas和HTML覆盖层
  • Rollup入门与进阶:为现代Web应用构建超小的打包文件
  • Q1财报持续向好,腾讯音乐如何在不确定中寻找确定性?
  • FlashInfer - 安装
  • Spark自定义分区器-基础
  • 订单服务拆分库表迁移实践
  • 杰理-701-手表sdk无法电脑连接经典蓝牙
  • calico.yaml+国内源
  • 《Effective Python》第2章 字符串和切片操作——深入理解Python 中的字符数据类型(bytes 与 str)的差异
  • Day1 时间复杂度
  • 【深度学习-Day 10】机器学习基石:从零入门线性回归与逻辑回归
  • 云共享虚拟主机具体是指什么?
  • “追光”植物背后的故事(二)
  • SpringBoot--springboot简述及快速入门
  • 基于 PLC 的轮式服务机器人研究
  • 医疗实时操作系统方案:手术机器人的微秒级运动控制