Advertisement

Replace texts in a string(string.h)

Started by November 13, 2002 12:03 PM
2 comments, last by Sand_Hawk 22 years ago
I am using string.h to store some strings. However, I want to run a replace on the stored string and replace all ''_''s with spaces. I tried looking up basic_string::replace() but that one is pretty weird. Is there a standard to do this? Sand Hawk ---------------- -Earth is 98% full. Please delete anybody you can.
My Site
----------------(Inspired by Pouya)
Replace will replace a number of characters at some point in the string with a string. As for how to do it, I''m fairly new to STL. There might be a transform function somewhere. . .
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
I found out how, look up the stl at www.sgi.com. With the function find(string What, int pos = 0) you search for a item and with replace(int StartPos, int CharCount, string What) you replace text. It''s easy.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site
----------------(Inspired by Pouya)
Darn, I thought this was the answer.


  #include <iostream>#include <string>#include <algorithm>char func(char c) {  return c == ''_'' ? '' '' : c; }int main(void) {  string s = "This_is_a_test.";  transform(s.begin(),s.end(), s.begin(), func);  cout << s;  return 0; }  
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement