Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jan 10th, 2007, 10:21 AM   #1
magnus.therning
Hobbyist Programmer
 
magnus.therning's Avatar
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 101
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
Old Jan 10th, 2007, 10:39 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
got terribly long errors
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?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 10th, 2007, 10:46 AM   #3
magnus.therning
Hobbyist Programmer
 
magnus.therning's Avatar
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 101
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
Old Jan 10th, 2007, 1:03 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Really? :beard:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 10th, 2007, 8:57 PM   #5
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
Quote:
Originally Posted by magnus.therning View Post
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.
I'd say that this is far more 'C++' ..
    std::string my_string("The quick brown fox jumped over the lazy dog");
    std::replace( my_string.begin(), my_string.end(), 'o', '*' );
Bench is offline   Reply With Quote
Old Jan 10th, 2007, 9:10 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Perhaps "C++" has come to mean "elegant" or similar while I was busy pondering "Pythonic". Neither example is C or Brainfuck.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 13th, 2007, 5:25 AM   #7
magnus.therning
Hobbyist Programmer
 
magnus.therning's Avatar
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 101
Rep Power: 3 magnus.therning is on a distinguished road
Quote:
Originally Posted by Bench View Post
I'd say that this is far more 'C++' ..
    std::string my_string("The quick brown fox jumped over the lazy dog");
    std::replace( my_string.begin(), my_string.end(), 'o', '*' );
Yes, that is a lot better. Thanks!
__________________
Don't comment bad code - rewrite it.
- The Elements of Programming Style (Kernighan & Plaugher)
magnus.therning is offline   Reply With Quote
Old Jan 13th, 2007, 6:09 AM   #8
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 343
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
@magnus.therning

dont do assignments in a while statment like that. the only thing that should be going on in the while(...) is a boolean test condtion.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Jan 13th, 2007, 8:09 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,315
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by mrynit View Post
dont do assignments in a while statment like that. the only thing that should be going on in the while(...) is a boolean test condtion.
There is no "should" about it, unless you are working with a zealous style guide.

There is nothing technically wrong with doing an assignment within the while expression.

Non-technically, it is often viewed as bad style because it is possible to introduce unintended errors or problems with code maintenance. Particularly with careless programmers -- but careless programmers will probably not comply with style guidelines anyway.
grumpy is offline   Reply With Quote
Old Jan 15th, 2007, 3:37 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
dont do assignments in a while statment like that. the only thing that should be going on in the while(...) is a boolean test condtion.
int i = 0;
while (i < 10)
{
   myArray [i] = i;
   ++i;
}
Please send me the code that performs this function with a boolean test.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

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 8:13 PM.

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