How can I replace all occurrences of a character with another character in std string?

Discussion in 'C++' started by herman, Aug 30, 2007.

  1. herman

    herman Guest

    How can I replace all occurrences of a character with another
    character in std string?

    For example, I want to replace '/' with '+' in my std::string

    I have looked at the replace() method in the string class, it does not
    replace all occurrences of a character with another character.

    http://www.cppreference.com/cppstring/index.html

    Thank you
    herman, Aug 30, 2007
    #1
    1. Advertising

  2. herman

    Ian Collins Guest

    Re: How can I replace all occurrences of a character with anothercharacter in std string?

    herman wrote:
    > How can I replace all occurrences of a character with another
    > character in std string?
    >
    > For example, I want to replace '/' with '+' in my std::string
    >
    > I have looked at the replace() method in the string class, it does not
    > replace all occurrences of a character with another character.
    >

    You either have to roll your own, or look at a regular expression
    library like boost.regexp:

    http://www.boost.org/libs/regex/doc/introduction.html

    --
    Ian Collins.
    Ian Collins, Aug 30, 2007
    #2
    1. Advertising

  3. "herman" <> wrote in message
    news:...
    >
    > How can I replace all occurrences of a character with another
    > character in std string?
    >
    > For example, I want to replace '/' with '+' in my std::string


    Try using std::for_each with a functor. For example

    struct testfunctor
    {
    void operator()(char& c) { if(c == '/') c = '+'; }
    };
    ....
    std::string str("This is a test - /////////\n");
    std::cout << str;
    std::for_each( str.begin(), str.end(), testfunctor() );
    std::cout << str;
    Matthew Crisanti, Aug 30, 2007
    #3
  4. herman

    Guest

    On Aug 30, 9:39 am, herman <> wrote:
    > How can I replace all occurrences of a character with another
    > character in std string?
    >
    > For example, I want to replace '/' with '+' in my std::string
    >
    > I have looked at the replace() method in the string class, it does not
    > replace all occurrences of a character with another character.
    >
    > http://www.cppreference.com/cppstring/index.html
    >
    > Thank you


    var str;
    str.replace(///g,'+');

    /g is for universal char i.e all char in string.
    , Aug 30, 2007
    #4
  5. herman

    BobR Guest

    herman <> wrote in message...
    >
    > How can I replace all occurrences of a character with another
    > character in std string?
    >
    > For example, I want to replace '/' with '+' in my std::string
    >
    > I have looked at the replace() method in the string class, it does not
    > replace all occurrences of a character with another character.
    >
    > http://www.cppreference.com/cppstring/index.html
    >
    > Thank you


    // #include <algorithm>
    std::string rep( "?Hello?, ?World?" );
    std::replace( rep.begin(), rep.end(), '?', '+' );
    cout<<"replace ="<<rep<<std::endl;
    // out: replace =+Hello+, +World+

    --
    Bob R
    POVrookie
    BobR, Aug 30, 2007
    #5
  6. Re: How can I replace all occurrences of a character with anothercharacter in std string?

    On 2007-08-30 08:09, wrote:
    > On Aug 30, 9:39 am, herman <> wrote:
    >> How can I replace all occurrences of a character with another
    >> character in std string?
    >>
    >> For example, I want to replace '/' with '+' in my std::string
    >>
    >> I have looked at the replace() method in the string class, it does not
    >> replace all occurrences of a character with another character.
    >>
    >> http://www.cppreference.com/cppstring/index.html
    >>
    >> Thank you

    >
    > var str;
    > str.replace(///g,'+');
    >
    > /g is for universal char i.e all char in string.


    The other people in this group use the C++ language, what do you use?

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 30, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shabam
    Replies:
    2
    Views:
    2,019
    Joe Smith
    Nov 17, 2004
  2. yogi_bear_79
    Replies:
    9
    Views:
    4,972
    James Kanze
    Feb 28, 2008
  3. Dwight Army of Champions
    Replies:
    0
    Views:
    391
    Dwight Army of Champions
    Apr 20, 2010
  4. PerlFAQ Server
    Replies:
    0
    Views:
    159
    PerlFAQ Server
    Jan 4, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    148
    PerlFAQ Server
    Apr 22, 2011
Loading...

Share This Page