#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    14

    Replacing a character in a string


    The PHP equiv is this:

    PHP Code:

    $string 
    str_replace (" is"" was'"$string); 
    How would I write the code so that C++ would do the same operation?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    The ANSI standard string class has a replace method with the following three arguments:

    string.replace(start, length, newstring);

    This means you're going to have to use the find() method to find the position of the old substring:

    Code:
    #include <string>
    #include <iostream>
    int main() {
      string s = "This is the old string";
      cout << s << "\n";
      int start = s.find("old");
      int len = string("old").size();
      s.replace(start, len, "new");
      cout << s << "\n";
      return 0;
    }

    Most people write their own string class which has a replace method that works similar to how PHP does it. You can find a lot of those on the web (such as the GNU String class http://www.math.utah.edu/docs/info/libg++_19.html#SEC27). The function to replace strings like the php example is called gsub in this case.

    If you're using Visual C++ or Borland C++ Builder, the string classes are called CString and AnsiString respectively. For Visual C++, the CString class has a Replace method as in:
    s.Replace("old", "new");

    C++ Builder has a StringReplace function that works as follows:
    s = StringReplace(s, "old", "new", TReplaceFlags() << rfReplaceAll << rfIgnoreCase);

    Comments on this post

    • Randolpho agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    1
    Rep Power
    0
    Originally Posted by Scorpions4ever
    The ANSI standard string class has a replace method with the following three arguments:

    string.replace(start, length, newstring);

    This means you're going to have to use the find() method to find the position of the old substring:

    Code:
    #include <string>
    #include <iostream>
    int main() {
      string s = "This is the old string";
      cout << s << "\n";
      int start = s.find("old");
      int len = string("old").size();
      s.replace(start, len, "new");
      cout << s << "\n";
      return 0;
    }

    Most people write their own string class which has a replace method that works similar to how PHP does it. You can find a lot of those on the web (such as the GNU String class http://www.math.utah.edu/docs/info/libg++_19.html#SEC27). The function to replace strings like the php example is called gsub in this case.

    If you're using Visual C++ or Borland C++ Builder, the string classes are called CString and AnsiString respectively. For Visual C++, the CString class has a Replace method as in:
    s.Replace("old", "new");

    C++ Builder has a StringReplace function that works as follows:
    s = StringReplace(s, "old", "new", TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
    I'd like to transfer "A older old wife" to "A older new wife",
    i.e. with feature "whole word only", how can i do?
  6. #4
  7. No Profile Picture
    Redpill
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2005
    Posts
    1,660
    Rep Power
    151
    If your definition of "whole word" is "space delimited", then just add spaces to both sides of the parameters. Otherwise you'll need to look into C/C++ regexp libraries.
    Code:
    #include <stdio.h>
    int main(int o,char**O){return o>-1?o-2||!main(-1,1+O)?!!fprintf(stderr,"%s [0-"
    "9]{81}\n",*O):main(-83,++O):o>-83?(*O)[-1-o]?81==(o=-o-1)||o[*O]<'0'||'9'<o[*O]
    ?0:main(-2-o,O):o==-82:o>-164?(*O)[-83-o]<'1'?main(o-82,O):main(--o,O):o+164?o>-
    246?(*O)[-165-o]<'1'?main(o-82,O):main(--o,O):o+246?o>-328?(*O)[o=-o-247]<='8'?(
    main(-328-o,(++o[*O],O)),main(-247-o,O)):!(o[*O]='0'):(o=-o-328)<729?(o%9/3*3-o%
    27+o/243*9+o/81%3&&(*O)[o%81]==(*O)[o%81-o%27+o%9/3*3+o/243*9+o/81%3])||(o%81-o%
    9-o/81*9&&(*O)[o%81]==(*O)[o%9+o/81*9])||(o/81-o%9&&(*O)[o%81]==(*O)[o%81-o%9+o/
    81])?0:main(-409-o,O):main(-165-o%81,O):!puts(*O):0                           ;}

IMN logo majestic logo threadwatch logo seochat tools logo