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. python 全栈开发,Day82(点赞和踩灭,用户评论)

    一.点赞和踩灭 样式 先来做样式,修改article_detail.html,增加div_digg的div {% extends "base.html" %} {% block c ...

  2. 【spring基础】spring声明式事务详解

    一.spring声明式事务 1.1 spring的事务管理器 spring没有直接管理事务,而是将管理事务的责任委托给JTA或相应的持久性机制所提供的某个特定平台的事务实现.spring容器负责事物的 ...

  3. 最长上升序列 LCS LIS

    子序列问题 (一)一个序列中的最长上升子序列(LISLIS) n2做法 直接dp即可: ;i<=n;i++) { dp[i]=;//初始化 ;j<i;j++)//枚举i之前的每一个j ) ...

  4. wxpython安装,demo下载

    wxPython介绍      wxPython是Python语言的一套优秀的GUI图形库.wxPython可以很方便的创建完整的.功能键全的GUI用户界面. wxPython安装 本安装采用pip自 ...

  5. vue2.0以上版本安装sass(scss)

    一.首先说明sass和scss的区别. 1.异同:1)简言之可以理解scss是sass的一个升级版本,完全兼容sass之前的功能,又有了些新增能力.语法形式上有些许不同,最主要的就是sass是靠缩进表 ...

  6. IdentityServer4-用EF配置Client(一)

    一.背景 IdentityServer4的介绍将不再叙述,百度下可以找到,且官网的快速入门例子也有翻译的版本.这里主要从Client应用场景方面介绍对IdentityServer4的应用. 首先简要介 ...

  7. 获取img的高

    我们可以通过css设置图片的width,然后通过 clientWidth获取图片的宽,但是这个宽似乎是css里面定义的width值,但是对于图片的高,使用 clientHeight 来获取似乎是有些问 ...

  8. 三篇文章带你极速入门php(二)之迅速搭建php环境

    前言 今天讲一下php在windows,mac,linux上的集成环境搭建,目标是简单快速,环境这个事得对号入座,windows用phpstudy,mac用mamp,linux用lnmp一键安装,直接 ...

  9. Ubuntu crontab 定时 python 详细

    Ubuntu系统,定时执行python脚本. 目的:每分钟执行一次timer_test.py timer_test.py    路径 /home/li/d/pythonwork/test/timer_ ...

  10. [环境]vscode中python虚拟环境

    在项目.vscode/settings.json下设置 { "python.pythonPath": "/path/to/python2.7"}