HOW TO REPLACE ALL OCCURRENCES OF A CHARACTER IN A STD::STRING
From: http://www.martinbroadhurst.com/replacing-all-occurrences-of-a-character-in-a-stdstring.html
This can be done using the standard library or Boost. The advantage of using Boost is that you get Boost ranges, which mean that you don’t need to specify the beginning and end of the string.
With both libraries, the replacement can be made on the original string or a copy.
- Use std::replace()
- Use std::replace_copy
- Use boost_replace_all
- Use boost_replace_all_copy
Method 1: Use std::replace()
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string str("Quick+brown+fox"); std::replace(str.begin(), str.end(), '+', ' '); std::cout << str << "\n"; } |
Method 2: Use std::replace_copy
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string str1("Quick+brown+fox"); std::string str2(str1.size(), '\0'); std::replace_copy(str1.begin(), str1.end(), str2.begin(), '+', ' '); std::cout << str2 << "\n"; } |
Method 3: Use boost_replace_all
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> #include <string> #include <boost/algorithm/string/replace.hpp> int main() { std::string str("Quick+brown+fox"); boost::replace_all(str, "+", " "); std::cout << str << "\n"; } |
Method 4: Use boost_replace_all_copy
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> #include <string> #include <boost/algorithm/string/replace.hpp> int main() { std::string str1("Quick+brown+fox"); std::string str2 = boost::replace_all_copy(str1, "+", " "); std::cout << str2 << "\n"; } |
HOW TO REPLACE ALL OCCURRENCES OF A CHARACTER IN A STD::STRING的更多相关文章
- Replace - with an en dash character (–, –) ?
这个安卓开发过程中eclipse的提示,新浪网友给出这个解决方法:http://blog.sina.com.cn/s/blog_5ea8670101015dgk.html 太笨了. 看看stacko ...
- Unicode与ASCiI之间有什么区别?java当中的转义字符 Character类的使用 String类的使用
ASCII码 称为 美国标准信息交换码 (American standard code of Information Interchange) 其中一共有多少个码?2的7次幂 128个 Unicode ...
- Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
1. 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...
- spring 里面的StringUtils,先放这儿,有时间研究吧
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Vers ...
- C++string中用于查找的find系列函数浅析
总述: 以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算).若查找成功,返回按查找规则找到的第一个字符或子串的位置:若查找 ...
- C++ 经常使用类 string类
===6.3.2使用string对象=== string word="I love China" *链接字符串* string description=adjective + & ...
- 类string解析
原创作品,转载请注明来源:http://www.cnblogs.com/shrimp-can/p/5645248.html 在涉及字符串的时候,我们可以定义字符数组或指针,其实还有一个类,专门是为字符 ...
- Parameter pack
Parameter pack C++ C++ language Templates A template parameter pack is a template parameter ...
- 用于模式匹配的String方法和RegExp方法
上一节总结了创建正则表达式的语法,这一篇笔者总结了用于模式匹配的String四个方法:search().replace().match().split()以及用于模式匹配的RegExp两个方法exec ...
随机推荐
- JavaStrip和python的变量存储位置
<script> var a =1 function a() { console.log("sdfdsf") } a() </script> Uncaugh ...
- ERP产品销售发货判断库存功能(四十二)
产品数量的前端(键盘抬起的事件): <td> <input type="text" name="proCount" onkeyup=" ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- VS增加插件 Supercharger破解教程
VS增加插件 Supercharger破解教程 Supercharger效果预览及下载路径: http://supercharger.tools/index.html 下载地址:https://vis ...
- mysql explain优化
简介 MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT 语句进行分析, 并输出 SELECT 执行的详细信息, 以供开发人员针对性优化.EXPLAIN 命令用法十分简单, 在 S ...
- python日期与时间
1.介绍 Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表 ...
- Redis持久化(persistence)
Redis 持久化 Redis 提供了多种不同级别的持久化方式: RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AOF 持久化记录服务器 ...
- Python库资源大全
转载地址:https://zhuanlan.zhihu.com/p/27350980 本文是一个精心设计的Python框架.库.软件和资源列表,是一个Awesome XXX系列的资源整理,由BigQu ...
- poj2230 Watchcow【欧拉回路】【输出路径】(遍历所有边的两个方向)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4392 题目大意: 一个图,要将每条边恰好遍历两遍,而且要以不同的方向,还要回到原点. dfs解法 ...
- P1939【模板】矩阵加速(数列)
P1939[模板]矩阵加速(数列)难受就难受在a[i-3],这样的话让k=3就好了. #include<iostream> #include<cstdio> #include& ...