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 ...
随机推荐
- LeetCode(28): 实现strStr()
Easy! 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0 ...
- Laravel Eloquent 数据查询结果中日期的格式化
两种情况: 使用 Model 的查询 例如: $item = App\Models\Apple::first(); $date = $item->created_at->format('Y ...
- ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...
- 模拟js中注册表单验证
示例1 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- python3 + selenium 多iframe(框架)切换
html演示: frame.html: <html> <head> <meta http-equiv="content-type" content=& ...
- python 全栈开发,Day107(CRM初始,权限组件之权限控制,权限系统表设计)
一.CRM初始 CRM,客户关系管理系统(Customer Relationship Management).企业用CRM技术来管理与客户之间的关系,以求提升企业成功的管理方式,其目的是协助企业管理销 ...
- 【转】ssh服务器启动和客户端常用操作
前言 简单的来说,SSH 是 Secure SHell protocol 的简写 (安全的壳程序协议),它可以透过数据封包加密技术,将等待传输的封包加密后再传输到网络上, 因此,数据讯息当然就比较安全 ...
- spring配置redis注解缓存
前几天在spring整合Redis的时候使用了手动的方式,也就是可以手动的向redis添加缓存与清除缓存,参考:http://www.cnblogs.com/qlqwjy/p/8562703.html ...
- 高版本js实现live
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- URAL - 1495 One-two, One-two 2
URAL - 1495 这是在dp的专题里写了,想了半天的dp,其实就是暴力... 题目大意:给你一个n,问你在30位以内有没有一个只由1或2 构成的数被 n 整除,如果 有则输出最小的那个,否则输出 ...