0

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

std::replace( s.begin(), s.end(), ':', '%%' ); this code above doesn't work:

error no instance matches the arguement list

Thanks!

4

1 回答 1

8

不幸的是,没有办法:一次性替换所有角色。但是您可以循环执行,如下所示:

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

如果您只需要替换第一个冒号,则单独使用循环体,不要在其周围使用循环。

于 2013-01-01T18:52:51.000 回答