Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please Fix My Dll Kilo C++ 12 May 25th, 2006 12:21 PM
C# corruption!!! Kilo C++ 32 May 21st, 2006 9:44 PM
Testing for a palindrome using std::string Jessehk C++ 9 May 3rd, 2006 2:55 AM
2 dimension array of characters brad sue C 8 Mar 15th, 2006 10:40 AM
Converting ANSI characters to hex for Checksum. JawaKing00 C 4 Sep 9th, 2005 6:07 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC