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 ...
随机推荐
- 一个tomcat上部署多个项目,并通过不同端口号访问不同的项目
原文:http://www.cnblogs.com/kismetv/p/7228274.html#title3-1 现在以部署两个项目为例: 第一步:修改tomcat下的server.xml文件 配置 ...
- 《剑指offer》-链表的第一个公共节点
题目描述 输入两个链表,找出它们的第一个公共结点. 这题目是指针相关的题目.初步要判断出来,有公共节点的两个指针,应当是链表后半部分相同.这样的话,当遇到第一个相同节点(不是node的val相同,而是 ...
- js随机生成颜色的方法
function getRandomColor() { return '#' + (Math.random() * 0xffffff << 0).toString(16); }
- asp.net core web项目目录解读
Connected Services 和传统.net web项目相比,它的功能类似于添加webservice或者wcf service的引用.暂时用不到,有兴趣的小伙伴可以深入了解.右键这个目录可以看 ...
- python selenium-webdriver 元素定位(三)
上两篇的博文中介绍了python selenium的环境搭建和编写的第一个自动化测试脚本,从第二篇的例子中看出来再做UI级别的自动化测试的时候,有一个至关重要的因素,那就是元素的定位,只有从页面上找到 ...
- linux 重要笔记
nginx 服务器重启命令,关闭 nginx -s reload :修改配置后重新加载生效 nginx -s reopen :重新打开日志文件nginx -t -c /path/to/ngin ...
- 化学1(chem1)- 化学合成
P2784 化学1(chem1)- 化学合成 还是spfa,距离数组初始化为-1,松弛操作改为*就好了,一开始老是超时,后来加了一个visit数组就过了,这个重复造成的效率浪费还是蛮大的,以后都要加. ...
- 不一样的go语言-不同的语法之type
前言 在go语言中,type用于类型定义(type definition)与类型别名(type alias).这两者的差别从名字上已经可以初见端倪. 类型定义即定义新类型,是一个全新的类型,但 ...
- Python开发之日志记录模块:logging
1 引言 最近在开发一个应用软件,为方便调试和后期维护,在代码中添加了日志,用的是Python内置的logging模块,看了许多博主的博文,颇有所得.不得不说,有许多博主大牛总结得确实很好.似乎我再写 ...
- Project_Lemon测评系统安装经验
历经千辛万苦才在我自己的Linux上装好了Lemon 因为毕竟没有什么使用Linux的经验然后踩了不少坑,同时为了所以就有了这篇文章. 本教程大部分都基于Linux,若有需要Windows下的帮助请看 ...