用另外两个替换字符 [英] Replace the character with two other

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

问题描述

我有一个 std::string,如何用 %% 替换 : 字符?

I have a std::string, how can i replace : character with %%?

std::replace(s.begin(), s.end(), ':', '%%' );上面的代码不起作用:

错误没有实例匹配论证列表

error no instance matches the arguement list

谢谢!

推荐答案

遗憾的是,无法一次性替换所有 : 字符.但是你可以循环执行,就像这样:

Unfortunately, there is no way to replace all : characters in one shot. But you can do it in a loop, like this:

string s = "quick:brown:fox:jumps:over:the:lazy:dog";
int i = 0;
for (;;) {
    i = s.find(":", i);
    if (i == string::npos) {
        break;
    }
    s.replace(i, 1, "%%");
}
cout << s << endl;

这个程序打印

quick%%brown%%fox%%jumps%%over%%the%%lazy%%dog

如果您只需要替换第一个冒号,则单独使用循环体,不要使用循环体.

If you need to replace only the first colon, then use the body of the loop by itself, without the loop around it.

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

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