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

相关文章

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串