如何替换字符串中所有出现的字符? [英] How to replace all occurrences of a character in string?

查看:148
本文介绍了如何替换字符串中所有出现的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: string ?中用另一个字符替换字符的所有出现的有效方式是什么?

std :: string 不包含这样的函数,但你可以使用独立的 replace

$

$

  < algorithm> 
#include< string>

void some_func(){
std :: string s =example string;
std :: replace(s.begin(),s.end(),'x','y'); // replace all'x'to'y'
}


What is the effective way to replace all occurrences of a character with another character in std::string?

解决方案

std::string doesn't contain such function but you could use stand-alone replace function from algorithm header.

#include <algorithm>
#include <string>

void some_func() {
  std::string s = "example string";
  std::replace( s.begin(), s.end(), 'x', 'y'); // replace all 'x' to 'y'
}

这篇关于如何替换字符串中所有出现的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆