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. pyhon----模块导入

    正常情况 报错(两个导入都报错)

  2. cf343c 二分答案+模拟

    /* 怎么判断能否在时间k内完成扫描 贪心:每次取出最靠左边的磁头去扫描最左边的,然后再往右扫描即可 如果当前点无法扫到最左侧点,那么后继点一样无法扫到 */ #include<bits/std ...

  3. SPLAY,LCT学习笔记(三)

    前两篇讲述了SPLAY模板操作,这一篇稍微介绍一下SPLAY的实际应用 (其实只有一道题,因为本蒟蒻就写了这一个) 例:bzoj 1014火星人prefix 由于本蒟蒻不会后缀数组,所以题目中给的提示 ...

  4. SqlServer基础语句练习(一)

    学了不少东西,感觉自己的sql语句还是很不好,从基础学起吧. 来一段sql脚本: create database tongji go use tongji go create table studen ...

  5. SPFILEOPENBANKDB.ORA 手动编辑产生问题

    因为最近启动后发现经常内存高占用,一个ORACLE实例占用超过7G内存,两个就15G,卡的让人坐立不安.于是百度了一下,使用下面的命令将sga_max_size从7G修改为200M show para ...

  6. Linux环境下Node.js的安装配置

    1.   官网下载Node.js 2.   安装Node.js 根据下载内容的不同,提供三种安装方法,选择自己喜欢的方式 2.1.   绿色免安装版(Linux(.tar.gz)) 解压Node-XX ...

  7. POJ 2184 Cow Exhibition (带负值的01背包)

    题意:给你N(N<=100)只牛,每只牛有一个智慧值Si和一个活泼值Fi,现在要从中找出一些来,使得这些牛智慧值总和S与活泼值总和F之和最大,且F和S均为正.Si和Fi范围在-1000到1000 ...

  8. python函数式编程——匿名函数(lambda)

    匿名函数lambda lambda x:x*x x就是参数 相当于函数 def f(x): return x*x 匿名函数可以作为函数对象赋值给变量: >>> f = lambda ...

  9. python中的协程:greenlet和gevent

    python中的协程:greenlet和gevent 协程是一中多任务实现方式,它不需要多个进程或线程就可以实现多任务. 1.通过yield实现协程: 代码: import time def A(): ...

  10. 026 使用大数据对网站基本指标PV案例的分析

    案例: 使用电商网站的用户行为日志进行统计分析 一:准备 1.指标 PV:网页流浪量 UV:独立访客数 VV:访客的访问数,session次数 IP:独立的IP数 2.上传测试数据 3.查看第一条记录 ...