码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++ string replace操作


    C++ string replace操作本来网上有很多,但是按其操作有坑,编译提示语法错误。所以特此记录:
    参考链接(有坑):

    目录

      • 1、单个字符替换
        • 1.1、单个字符替换
        • 1.2、延申1:一个字符串向后面替换多个字符串测试代码:
        • 1.3、延申2:多个字符串向后面替换多个字符串测试代码:
      • 2、字符串替换
        • 2.1、字符串替换
        • 2.2、使用字符串(长度小于原字符串)来替换单字符,测试代码:
        • 2.3、使用字符串(长度大于原字符串)来替换单字符,测试代码:
        • 2.4、使用字符串来替换字符串,测试代码:

    1、单个字符替换

    1.1、单个字符替换

    这里有一个需求,把路径中所有正斜杠改成反斜杠(/ --> \)

    replace(起始位置,替换字符长度,待替换字符长度,待替换字符)
    Demo如下:

    #include 
    #include 
    #include 
    
    using namespace std;
    int main()
    {
    	string outPath = "F:/11JIAMIEXE/2Bin/";;//测试用1个字符串替换一个字符串的效果
    	printf("Outpath1:%s\n", outPath.c_str());
    	while (outPath.find('/') != outPath.npos)
    	{
    		outPath = outPath.replace(outPath.find('/'),1,1, '\\');//测试用1个字符串替换一个字符串的效果。起始位置、终止位置、替换字符串个数、替换的字符串
    		printf("Outpath2:%s\n", outPath.c_str());
    	}
    	printf("Result:%s\n", outPath.c_str());
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    结果如下:

    在这里插入图片描述

    1.2、延申1:一个字符串向后面替换多个字符串测试代码:

    下面代码是一个字符串向后面替换2个字符串测试demo:

    #include 
    #include 
    #include 
    
    using namespace std;
    int main()
    {
    	string outPath = "F:/11JIAMIEXE/2Bin/";
    	printf("Outpath1:%s\n", outPath.c_str());
    
    	//auto startSize = outPath.find('/');
    	while (outPath.find('/') != outPath.npos)
    	{
    		outPath = outPath.replace(outPath.find('/'),2,1, '\\');//测试用1个字符串替换两个字符串。起始位置、终止位置、替换字符串个数、替换的字符串
    		printf("Outpath2:%s\n", outPath.c_str());
    	}
    	printf("Result:%s\n", outPath.c_str());
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    输出结果:
    在这里插入图片描述

    下面代码是一个字符串向后面替换3个字符串测试demo,只用把结束字符串数改成3即可,下过如下:
    在这里插入图片描述

    1.3、延申2:多个字符串向后面替换多个字符串测试代码:

    接着上面的代码进行测试,使用3个字符串向后面替换3个字符串测试demo,只用把结束字符串数改成3即可,下过如下:
    在这里插入图片描述
    经过测试单字符设置多个字符串替换的时候只是复制了多次该单字符

    2、字符串替换

    2.1、字符串替换

    字符串替换的方法略微变通一下即可:
    replace(起始位置,终止位置,字符串,字符串长度)
    使用字符串来替换单字符,测试代码:

    
    #include 
    #include 
    #include 
    
    using namespace std;
    int main()
    {
    	string outPath = "F:/11JIAMIEXE/2Bin/";//测试用1个字符串替换两个字符串
    
    	printf("Outpath1:%s\n", outPath.c_str());
    
    
    	while (outPath.find('/') != outPath.npos)
    	{
    		outPath = outPath.replace(outPath.find('/'),1, "ABC",3);//字符串替换
    		printf("Outpath2:%s\n", outPath.c_str());
    	}
    	printf("Result:%s\n", outPath.c_str());
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    结果如下图:
    在这里插入图片描述

    2.2、使用字符串(长度小于原字符串)来替换单字符,测试代码:

    在这里插入图片描述

    2.3、使用字符串(长度大于原字符串)来替换单字符,测试代码:

    在这里插入图片描述
    注意,结果是错的!切勿此操作

    2.4、使用字符串来替换字符串,测试代码:

    指定好覆盖长度即可,如下图:
    在这里插入图片描述
    以上,总结完毕!辉·2022.8.5

  • 相关阅读:
    groovy在SpringBoot中的使用
    2023 年全国大学生数学建模A题目-定日镜场的优化设计
    漏洞修复:在应用程序中发现不必要的 Http 响应头
    腾讯地图撒点并默认显示点位信息
    让代码变美的第二天 - 策略模式
    技术解读数据库如何实现“多租户”?
    Ubuntu22下载安装
    JTS:04 读取数据库数据
    CV库简介和函数说明
    Java技能树-网络-UDP-DatagramSocket
  • 原文地址:https://blog.csdn.net/weixin_42727069/article/details/126172630
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号