![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: May 2006
Location: Cambridge, UK
Posts: 101
Rep Power: 3
![]() |
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
Is there an easier way?
__________________
Don't comment bad code - rewrite it. - The Elements of Programming Style (Kernighan & Plaugher) |
|
|
|
|
|
#2 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
. 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 |
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: May 2006
Location: Cambridge, UK
Posts: 101
Rep Power: 3
![]() |
Quote:
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) |
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
Quote:
std::string my_string("The quick brown fox jumped over the lazy dog");
std::replace( my_string.begin(), my_string.end(), 'o', '*' );![]() |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: May 2006
Location: Cambridge, UK
Posts: 101
Rep Power: 3
![]() |
Quote:
__________________
Don't comment bad code - rewrite it. - The Elements of Programming Style (Kernighan & Plaugher) |
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
@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 |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,315
Rep Power: 5
![]() |
Quote:
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. |
|
|
|
|
|
|
#10 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
int i = 0;
while (i < 10)
{
myArray [i] = i;
++i;
}
__________________
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |