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.

  1. Use std::replace()
  2. Use std::replace_copy
  3. Use boost_replace_all
  4. 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的更多相关文章

  1. Replace - with an en dash character (–, –) ?

    这个安卓开发过程中eclipse的提示,新浪网友给出这个解决方法:http://blog.sina.com.cn/s/blog_5ea8670101015dgk.html  太笨了. 看看stacko ...

  2. Unicode与ASCiI之间有什么区别?java当中的转义字符 Character类的使用 String类的使用

    ASCII码 称为 美国标准信息交换码 (American standard code of Information Interchange) 其中一共有多少个码?2的7次幂 128个 Unicode ...

  3. Java基础知识强化101:Java 中的 String对象真的不可变吗 ?

    1. 什么是不可变对象?       众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...

  4. spring 里面的StringUtils,先放这儿,有时间研究吧

    /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Vers ...

  5. C++string中用于查找的find系列函数浅析

    总述:      以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算).若查找成功,返回按查找规则找到的第一个字符或子串的位置:若查找 ...

  6. C++ 经常使用类 string类

    ===6.3.2使用string对象=== string word="I love China" *链接字符串* string description=adjective  + & ...

  7. 类string解析

    原创作品,转载请注明来源:http://www.cnblogs.com/shrimp-can/p/5645248.html 在涉及字符串的时候,我们可以定义字符数组或指针,其实还有一个类,专门是为字符 ...

  8. Parameter pack

    Parameter pack   C++   C++ language   Templates   A template parameter pack is a template parameter ...

  9. 用于模式匹配的String方法和RegExp方法

    上一节总结了创建正则表达式的语法,这一篇笔者总结了用于模式匹配的String四个方法:search().replace().match().split()以及用于模式匹配的RegExp两个方法exec ...

随机推荐

  1. 《剑指offer》-中序遍历下一个节点

    题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. /* struct TreeLinkNode { in ...

  2. Gitlab库已损坏前端显示500错误解决方法

    1.问题起因 办公网机房意外断掉,导致gitlab库文件损坏.开发打开gitlab显示500 2.查看日志 命令查看:gitlab-ctl tail 或者手动查看:/var/log/gitlab/gi ...

  3. 状压dp-----三进制

    三进制的状压dp要先预处理3^n以及每一个数的每一位 例题 hdu3001 题意: 给定n 个城市已经 m 条路 以及对应路费 c,要求遍历所有城市最少的路费,每个城市不能超过2次. 题解: 看代码吧 ...

  4. openstack学习-Horizon安装(八)

    一.安装Horizon [root@linux-node2 ~]# yum install -y openstack-dashboard 二.Horizon配置 [root@linux-node2 ~ ...

  5. python全栈开发day16-正则表达式和re模块

    1.昨日内容回顾 2.正则表达式(re模块是python中和正则表达式相关的模块) 1.作用 1).输入字符串是否符合匹配条件 2).从大段文字中匹配出符合条件的内容 2.字符组 [0-9a-zA-Z ...

  6. 【Java】 剑指offer(43) 从1到n整数中1出现的次数

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例 ...

  7. 010 Spark中的监控----日志聚合的配置,以及REST Api

    一:History日志聚合的配置 1.介绍 Spark的日志聚合功能不是standalone模式独享的,是所有运行模式下都会存在的情况 默认情况下历史日志是保存到tmp文件夹中的 2.参考官网的知识点 ...

  8. linux 编译内核 /boot空间不足?

    /boot空间一般分配100M的空间,本来是够用的,由于频繁的更新导致旧的不再使用的内核文件也保留在boot空间里,占着位置,所以把这些不用的内核文件下载掉boot空间就能释放出一部分, 具体做法为 ...

  9. [转]使用python来操作redis用法详解

    转自:使用python来操作redis用法详解 class CommRedisBase(): def __init__(self): REDIS_CONF = {} connection_pool = ...

  10. poj2186-Popular Cows【Tarjan】+(染色+缩点)(经典)

    <题目链接> 题目大意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但 ...