View Single Post
Old Jan 10th, 2007, 10:46 AM   #3
magnus.therning
Programmer
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 60
Rep Power: 3 magnus.therning is on a distinguished road
Quote:
Originally Posted by DaWei View Post
Nice, specific explanation. . Are you replacing characters or sets of characters with characters or sets of characters of the same length, or will your substitutions require adjustments in length?
LOL, yes, I know. Well, I find all errors due to type problems in STL being amazingly non-specific. Usually it's better to stare long and hard at the code rather then trying to decipher the message itself.

Anyway, what I want to do is replace all occurances of a specific character with another character. I.e. the simplest case possible.

I found that the following code works:

int pos;
while((pos = my_string.find_first_of(" ")) != std::string::npos)
  my_string[pos] = '_';

But, very C++ it isn't.
__________________
Don't comment bad code - rewrite it.
- The Elements of Programming Style (Kernighan & Plaugher)
magnus.therning is offline   Reply With Quote