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!

0

1 Answer 1

8

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; 

This program prints

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.