HOW TO REPLACE ALL OCCURRENCES OF A CHARACTER IN A STD::STRING
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.
- Use std::replace()
- Use std::replace_copy
- Use boost_replace_all
- 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的更多相关文章
- Replace - with an en dash character (–, –) ?
这个安卓开发过程中eclipse的提示,新浪网友给出这个解决方法:http://blog.sina.com.cn/s/blog_5ea8670101015dgk.html 太笨了. 看看stacko ...
- Unicode与ASCiI之间有什么区别?java当中的转义字符 Character类的使用 String类的使用
ASCII码 称为 美国标准信息交换码 (American standard code of Information Interchange) 其中一共有多少个码?2的7次幂 128个 Unicode ...
- Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
1. 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...
- spring 里面的StringUtils,先放这儿,有时间研究吧
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Vers ...
- C++string中用于查找的find系列函数浅析
总述: 以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算).若查找成功,返回按查找规则找到的第一个字符或子串的位置:若查找 ...
- C++ 经常使用类 string类
===6.3.2使用string对象=== string word="I love China" *链接字符串* string description=adjective + & ...
- 类string解析
原创作品,转载请注明来源:http://www.cnblogs.com/shrimp-can/p/5645248.html 在涉及字符串的时候,我们可以定义字符数组或指针,其实还有一个类,专门是为字符 ...
- Parameter pack
Parameter pack C++ C++ language Templates A template parameter pack is a template parameter ...
- 用于模式匹配的String方法和RegExp方法
上一节总结了创建正则表达式的语法,这一篇笔者总结了用于模式匹配的String四个方法:search().replace().match().split()以及用于模式匹配的RegExp两个方法exec ...
随机推荐
- cf 1082abc
还是菜,两题dp一题模拟 /* 反正就两个方向,往左或者往右,如果都不行,那就是-1 */ #include<bits/stdc++.h> using namespace std; int ...
- js 事件对象
/* 事件绑定的格式: 元素节点.on + 事件类型 = function(){ } 元素节点 事件类型 on+事件类型:事件处理函数 [注]上述三者一绑定:生成一个新的事件对象. [注]触发事件以后 ...
- 步步为营-63-Asp.net-get与post
1 get Get方式将数据发送到服务端,那么会将用户在表单中的数据放置到浏览器的地址栏中发送到服务器 格式:表单元素name属性的值=用户输入的值 请求地址:http://localhost:594 ...
- 《剑指offer》青蛙跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 很裸的斐波那契数列. class Solution { public: int jumpFloor ...
- hihocoder 编程练习赛23
第一题:H国的身份证号码I 题意:一个N位的正整数(首位不能是0).每位数字都小于等于K,并且任意相邻两位数字的乘积也小于等于K.按从小到大的顺序输出所有合法的N位号码,每个号码占一行. 思路:dfs ...
- bzoj3758. 数数
题解: 一波优秀的打表技巧 分块打表,分成1000组,打表打出来 另外10^6暴力算
- Linux dnsmasq.conf
一.配置文件:局域网内使用此dns服务时候首先会在host.dnsmasp里面找对应域名,若找不到则在resolv.dnsmasq中找 [root@operation_server dnsmasq.d ...
- 7-4素数环 uva 524
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using ...
- 017 Spark的运行模式(yarn模式)
1.关于mapreduce on yarn 来提交job的流程 yarn=resourcemanager(RM)+nodemanager(NM) client向RM提交任务 RM向NM分配applic ...
- 关于 C++ STL
一.STL简介 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Meng Lee和David R ...