View Single Post
Old Jan 10th, 2007, 10:21 AM   #1
magnus.therning
Programmer
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 60
Rep Power: 3 magnus.therning is on a distinguished road
Replacing characters in std::string

How can I easily replace characters in a std::string with another character?

Basically what I'd like is something like gsub (an addition to the string class in GNU's C++ class library). But I'm stuck on the Windows platform, and strongly encouraged to use the standard libraries as far as possible.

First I tried using string::iterator and indexing, but got terribly long errors when doing that.

Now my working solution is something like
  1. Convert it to a C-string (string::copy)
  2. Iterate over C-string, replacing characters
  3. Stick the C-string back into the C++-string

Is there an easier way?
__________________
Don't comment bad code - rewrite it.
- The Elements of Programming Style (Kernighan & Plaugher)
magnus.therning is offline   Reply With Quote