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. ERP产品采购申请管理(三十八)

    BLL层代码: public class BioPurchaseBLL { /// <summary> /// 购进申请添加 /// </summary> /// <pa ...

  2. Javascript面向对象基础(二)

    一: 用定义函数的方式定义类在面向对象的思想中,最核心的概念之一就是类.一个类表示了具有相似性质的一类事物的抽象,通过实例化一个类,可以获得属于该类的一个实例,即对象.在JavaScript中定义一个 ...

  3. 依赖倒置原则(Dependence Inversion Principle,DIP)

    依赖倒转原则就是 A.要依赖于抽象,不要依赖于实现.(Abstractions should not depend upon details. Details should depend upon a ...

  4. java 泛型 ? 和 T的区别

    看了一个CSDN的问题,感觉就清楚了:http://bbs.csdn.net/topics/300181589/ 摘录其中的重点: 泛型方法: public <T extends Object& ...

  5. Codeforces 1000F One Occurrence 主席树|| 离线+线段树

    One Occurrence 为什么我半年前这么菜呀, 这种场只A三题... 我们在主席树 || 线段树上维护每个数的右边和它一样的数在哪里, 然后就变成了区间求最大值. 注意加进去的时候要把它右边一 ...

  6. FastAdmin 在 Nginx 中的配置

    FastAdmin 使用的是 ThinkPHP 5 框架. 在 Apache 下很简单,但是在 nginx 下就需要自行配置一下了. 在网上找了很多,都没有找到很好的. 刚刚在 QQ 里群里找到一个, ...

  7. macbook安装并破解Clion2018(Pycharm也一样)

    一.下载 Clion 并安装 二.修改hosts文件,使用 vim /private/etc/hosts 命令将 0.0.0.0 account.jetbrains.com 粘贴进去(提示: i键是插 ...

  8. shell find

    find   -name april*                     在当前目录下查找以april开始的文件 find    /   -amin    -10     # 查找在系统中最后1 ...

  9. Linux/Window 正斜杠 反斜杠

    文件目录结构: Linux 是用正斜杠 目录名区分大小写 Window 是用反斜杠 目录名不区分大小写

  10. VB.NET只允许打开一个实例

    If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) ...