当前位置: 首页 > 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.xdnf.cn/news/421381.html

相关文章:

  • Rollup入门与进阶:为现代Web应用构建超小的打包文件
  • 【23种设计模式】分类结构有哪些?
  • Java——集合基础
  • OpenCV中的光流估计方法详解
  • 前端面试每日三题 - Day 33
  • 深入理解BLP安全模型:信息安全中的“守密者”
  • win部署Jenkins 自动化部署发布后端项目
  • 文件操作: File 类的用法和 InputStream, OutputStream 的用法
  • 构建媲美 ChatGPT 的 AI 交互界面—OpenWebUI
  • 大模型分布式光伏功率预测实现详解
  • Linux—进度条实现
  • 开源网络地图可视化第六章学习指南
  • 【unity游戏开发——编辑器扩展】使用MenuItem自定义菜单栏拓展
  • 【ArcGIS】根据shp范围生成系列等距点:范围外等距点+渔网点(Python全代码)
  • Android之横向滑动列表
  • ETL背景介绍_1:数据孤岛仓库的介绍
  • HTTP和HTTPS模块
  • 【计算机视觉】OpenCV实战项目:基于Tesseract与OpenCV的字符识别系统深度解析
  • Flask+HTML+Jquery 文件上传下载
  • 数据库优化
  • 第一章:HTML基石·现实的骨架
  • 《Effective Python》第2章 字符串和切片操作——深入理解Python 中的字符数据类型(bytes 与 str)的差异
  • ValueError: Caught ValueError in DataLoader worker process 0.
  • Postman启动时检测到版本不匹配错误
  • stm32之PWR、WDG
  • UE5通过C++实现TcpSocket连接
  • 在SQL Server中调整查询超时j解决方案
  • STM32 启动文件分析
  • 为何选择MCP?自建流程与Anthropic MCP的对比分析
  • docker大镜像优化实战