boost::string_algo详解6——replace相关函数

2024-06-15 03:48

本文主要是介绍boost::string_algo详解6——replace相关函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

replace的主要函数(以及其包括的copy函数)包括:
replace_range, replace_first, replace_last, replace_nth, replace_head, replace_tail, replace_regex, replace_all, replace_all_regex

[cpp]  view plain copy print ?
  1. void test_string_replace()  
  2. {  
  3.     using namespace boost;  
  4.     std::string str = "Hello Dolly, Hello World!";  
  5.   
  6.     std::cout << "#### replace_range ####" << std::endl;  
  7.     {  
  8.         std::string str1 = str;  
  9.         replace_range(str1, make_iterator_range(str1.begin() + 2, str1.end() - 2), "!!!");  
  10.         std::cout << str1 << std::endl;  
  11.     }  
  12.     std::cout << std::endl;  
  13.   
  14.     std::cout << "#### replace_first and replace_first_copy ####" << std::endl;  
  15.     {  
  16.         std::string str2 = str;  
  17.         replace_first(str2, "Hello""GoodBye");  
  18.         std::cout << str2 << std::endl;  
  19.     }  
  20.     {  
  21.         std::string str2 = str;  
  22.         std::string strc1 = replace_first_copy(str2, "Hello""GoodBye");  
  23.         std::cout << strc1 << std::endl;  
  24.   
  25.         std::string strc2 = "result = ";  
  26.         replace_first_copy(back_inserter(strc2), str, "Hello""GoodBye");  
  27.         std::cout << strc2 << std::endl;  
  28.     }  
  29.     std::cout << std::endl;  
  30.   
  31.     std::cout << "#### replace_last and replace_last_copy ####" << std::endl;  
  32.     {  
  33.         std::string str3 = str;  
  34.         replace_last(str3, "Hello""GoodBye");  
  35.         std::cout << str3 << std::endl;  
  36.     }  
  37.     {  
  38.         std::string str3 = str;  
  39.         std::string strc1 = replace_last_copy(str3, "Hello""GoodBye");  
  40.         std::cout << strc1 << std::endl;  
  41.   
  42.         std::string strc2 = "result = ";  
  43.         replace_last_copy(back_inserter(strc2), str, "Hello""GoodBye");  
  44.         std::cout << strc2 << std::endl;  
  45.     }  
  46.     std::cout << std::endl;  
  47.   
  48.     // 注意nth的索引从0开始.  
  49.     std::cout << "#### replace_nth and replace_nth_copy ####" << std::endl;  
  50.     {  
  51.         std::string str4 = str;  
  52.         replace_nth(str4, "Hello", 1, "GoodBye");  
  53.         std::cout << str4 << std::endl;  
  54.     }  
  55.     {  
  56.         std::string str4 = str;  
  57.         std::string strc1 = replace_nth_copy(str4, "Hello", 1, "GoodBye");  
  58.         std::cout << strc1 << std::endl;  
  59.   
  60.         std::string strc2 = "result = ";  
  61.         replace_nth_copy(back_inserter(strc2), str, "Hello", 1, "GoodBye");  
  62.         std::cout << strc2 << std::endl;  
  63.     }  
  64.     std::cout << std::endl;  
  65.   
  66.     std::cout << "#### replace_all and replace_all_copy ####" << std::endl;  
  67.     {  
  68.         std::string str5 = str;  
  69.         replace_all(str5, "Hello""GoodBye");  
  70.         std::cout << str5 << std::endl;  
  71.     }  
  72.     {  
  73.         std::string str5 = str;  
  74.         std::string strc1 = replace_all_copy(str5, "Hello""GoodBye");  
  75.         std::cout << strc1 << std::endl;  
  76.   
  77.         std::string strc2 = "result = ";  
  78.         replace_all_copy(back_inserter(strc2), str, "Hello""GoodBye");  
  79.         std::cout << strc2 << std::endl;  
  80.     }  
  81.     std::cout << std::endl;  
  82.   
  83.     std::cout << "#### replace_head and replace_head_copy ####" << std::endl;  
  84.     {  
  85.         std::string str6 = str;  
  86.         replace_head(str6, 5, "GoodBye");  
  87.         std::cout << str6 << std::endl;  
  88.     }  
  89.     {  
  90.         std::string str6 = str;  
  91.         std::string strc1 = replace_head_copy(str6, 5, "GoodBye");  
  92.         std::cout << strc1 << std::endl;  
  93.   
  94.         std::string strc2 = "result = ";  
  95.         replace_head_copy(back_inserter(strc2), str, 5, "GoodBye");  
  96.         std::cout << strc2 << std::endl;  
  97.     }  
  98.     std::cout << std::endl;  
  99.   
  100.     std::cout << "#### replace_tail and replace_tail_copy ####" << std::endl;  
  101.     {  
  102.         std::string str6 = str;  
  103.         replace_tail(str6, 6, "GoodBye");  
  104.         std::cout << str6 << std::endl;  
  105.     }  
  106.     {  
  107.         std::string str6 = str;  
  108.         std::string strc1 = replace_tail_copy(str6, 6, "GoodBye");  
  109.         std::cout << strc1 << std::endl;  
  110.   
  111.         std::string strc2 = "result = ";  
  112.         replace_tail_copy(back_inserter(strc2), str, 6, "GoodBye");  
  113.         std::cout << strc2 << std::endl;  
  114.     }  
  115.     std::cout << std::endl;  
  116.   
  117.     std::cout << "#### replace_regex, replace_regex_copy ####" << std::endl;  
  118.     {  
  119.         std::string str6 = str;  
  120.         regex reg("H.*?o");  
  121.   
  122. //      replace_regex(str6, reg, "GoodBye");    // 直接这么写会报错. 要用如下的方法.  
  123.         std::string strReplace = "GoodBye";  
  124.         replace_regex(str6, reg, strReplace);  
  125.   
  126.         std::cout << str6 << std::endl;  
  127.     }  
  128.   
  129.     {  
  130.         std::string str6 = str;  
  131.         regex reg("H.*?o");  
  132.         std::string strReplace = "GoodBye";  
  133.         std::string strc1 = replace_regex_copy(str6, reg, strReplace);  
  134.         std::cout << strc1 << std::endl;  
  135.   
  136.         std::string strc2 = "result = ";  
  137.         replace_regex_copy(back_inserter(strc2), str, reg, strReplace);  
  138.         std::cout << strc2 << std::endl;  
  139.     }  
  140.     std::cout << std::endl;  
  141.   
  142.     std::cout << "#### replace_all_regex, replace_all_regex_copy ####" << std::endl;  
  143.     {  
  144.         std::string str6 = str;  
  145.         regex reg("H.*?o");  
  146.         std::string strReplace = "GoodBye";  
  147.         replace_all_regex(str6, reg, strReplace);  
  148.         std::cout << str6 << std::endl;  
  149.     }  
  150.     {  
  151.         std::string str6 = str;  
  152.         regex reg("H.*?o");  
  153.         std::string strReplace = "GoodBye";  
  154.         std::string strc1 = replace_all_regex_copy(str6, reg, strReplace);  
  155.         std::cout << strc1 << std::endl;  
  156.   
  157.         std::string strc2 = "result = ";  
  158.         replace_all_regex_copy(back_inserter(strc2), str, reg, strReplace);  
  159.         std::cout << strc2 << std::endl;  
  160.     }  
  161.     std::cout << std::endl;  
  162. }  

这篇关于boost::string_algo详解6——replace相关函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062349

相关文章

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Redis Pipeline(管道) 详解

《RedisPipeline(管道)详解》Pipeline管道是Redis提供的一种批量执行命令的机制,通过将多个命令一次性发送到服务器并统一接收响应,减少网络往返次数(RTT),显著提升执行效率... 目录Redis Pipeline 详解1. Pipeline 的核心概念2. 工作原理与性能提升3. 核

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl