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. 关于java中Stream理解

    关于java中Stream理解 Stream是什么 Stream:Java 8新增的接口,Stream可以认为是一个高级版本的Iterator.它代表着数据流,流中的数据元素的数量可以是有限的, 也可 ...

  2. python接口自动化测试十五:解决密码动态,无法登录情况

    解决问题:每次密码都是变化的,无法通过账号密码登录 (总不能每次去fiddler复制吧????) 解决思路: 1.先用selenium调用浏览器(不会selenium的自己想办法了), 2.登录后从浏 ...

  3. python 全栈开发,Day77(图书管理系统)

    一.图书管理系统 完整代码链接: https://github.com/py3study/bms_multi 本项目使用session来实现一个简单的图书管理系统 未登录不允许访问后台: 直接访问后台 ...

  4. docker 运行Django项目

    一.概述 已经写好了一个Django项目,需要将这个项目用docker封装一个镜像,使用k8s发布! 在封装并运行的过程中,发现了很多问题,这里会一一介绍! 二.时区问题 采用的是镜像是 ubuntu ...

  5. sql分组获取第一条或者最小一条值

    很多人都在问这个问题,现在记录一下 select id from test as a where id = (select min(b.id ) from test as b where a.pid ...

  6. ORACLE分页查询SQL语法——高效的分页

    --1:无ORDER BY排序的写法.(效率最高)--(经过测试,此方法成本最低,只嵌套一层,速度最快!即使查询的数据量再大,也几乎不受影响,速度依然!) SELECT * FROM (SELECT  ...

  7. Spring MVC的前端控制器模式

    前端控制器模式 spring mvc也是依赖servlet,所以spring mvc的请求处理是从一个servlet开始,这个servlet就是DispatcherServlet.前端控制器模式(Fr ...

  8. poj2230 Watchcow【欧拉回路】【输出路径】(遍历所有边的两个方向)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4392 题目大意: 一个图,要将每条边恰好遍历两遍,而且要以不同的方向,还要回到原点. dfs解法    ...

  9. spring中整合ssm框架注解版

    和xml版差不多,只不过创建对象的方式是由spring自动扫描包名,然后命名空间多一行context代码在application.xml中,然后将每个对象通过注解创建和注入: 直接上代码: 1.use ...

  10. ubuntu安装mysql 时未提示输入密码

    我在Ubuntu16.04版本中使用终端安装MySQL5.7时,按照度娘的教程,搜索如何安装,大多是如下代码: sudo apt-get install mysql-server sudo apt-g ...