string类自定义字符串替换函数replace
2024-10-01 11:31:10
#include <iostream>
#include <string>
using namespace std;
/*
* 函数功能:将string字符串中的某些字符替换成其他字符
* 参数说明: str 原字符串 strFind 需要替换字符串 strReplace 替换字符串
*/
string replace(string &str,const string &strFind,const string &strReplace)
{
int fsize = strFind.size();
int rsize = strReplace.size();
int pos = str.find(strFind,0);//从开始位置查找第一个符合的字符串,未找到返回str.npos
while (pos!=str.npos)
{
str.replace(pos,rsize,strReplace); //替换查找到的字符串
pos = str.find(strFind,pos+fsize); //继续查找
}
return str;
}
int main(int argc, char* argv[])
{
string str="abc[def[gef[ee[";
string strFind ="[";
string strReplace="(";
replace(str,strFind,strReplace); //将字符串str中的“[”替换为“(”
cout<<str<<endl;
return 0;
}
最新文章
- 解决Tomcat无法shutdown进程
- Android Log介绍
- C++ 什么是句柄?为什么会有句柄?HANDLE
- php 基本符号
- Java NIO 缓冲技术详解
- Arduino 串口篇 Arduino发送十六进制 send HEX via serial port RS232-to-USB to PC
- WIN2003 设置 OPENVPN 服务端
- ERROR:";org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for /config/topics/test"; when creating or deleting Kafka operations authorized through the Ranger policies
- 外网访问内网SpringBoot
- 从vue源码看props
- Atitit 支出分类表 会计科目(1)资产(2)负债(3)资本(4)收益(5)费用(成本) 资产分类表 attilax总结
- UIScrollView _getDelegateZoomView bug 经历
- iftop 命令
- post与get的区别
- C# 抽签小程序
- vue02—— 动画、组件、组件之间的数据通信
- 20155224 2016-2017-2 《Java程序设计》第3周学习总结
- 【转载】OLE控件在Direct3D中的渲染方法
- Spring Bean作用域实例
- axios请求报Uncaught (in promise) Error: Request failed with status code 404
热门文章
- LightOJ-1236 Pairs Forming LCM 唯一分解定理
- 栈(stack)--c实现(使用双链表)
- MySQL 数据还原
- caioj 1157 线性筛选素数
- docker环境下mysql参数修改
- HDU 4069 数独
- ztree实现根节点右击事件,显示添加删除
- C - The C Answer (2nd Edition) - Exercise 1-12
- hadoop-11-ambari-server安装
- 程序员之---C语言细节12(指针和数组细节,&;quot;//&;quot;的可移植性说明)