替换字符串中的\remove字符 [英] Replace\remove character in string

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

问题描述

string DelStr = "I! am! bored!";
string RepStr = "10/07/10"

我要删除所有'! 'on DelStr,我想用RepStr字符串中的' - '替换所有的'/'。

I want to delete all '!' on DelStr and I want to replace all '/' with '-' on the RepStr string.

有没有办法做这个,每个字符?

Is there any way to do this without doing a loop to go through each character?

推荐答案

移除感叹号:

#include <algorithm>
#include <iterator>

std::string result;
std::remove_copy(delStr.begin(), delStr.end(), std::back_inserter(result), '!');

或者,如果要打印字符串,则不需要 result 变量:

Alternatively, if you want to print the string, you don't need the result variable:

#include <iostream>

std::remove_copy(delStr.begin(), delStr.end(),
                 std::ostream_iterator<char>(std::cout), '!');

替换斜线的破折号:

std::replace(repStr.begin(), repStr.end(), '/', '-');

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

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