Remove Data from basic::string

Discussion in 'C++' started by Mike Copeland, Apr 1, 2009.

  1. What is a good way to remove a character from a basic::string?
    For example, I have the following:

    basic::string myString = "123 E, Main Street";

    and I wish to remove (delete) the "," from it.
    Does anyone know a way to do this? TIA
    Mike Copeland, Apr 1, 2009
    #1
    1. Advertising

  2. Mike Copeland

    Stefan Ram Guest

    (Mike Copeland) writes:
    >basic::string myString = "123 E, Main Street";
    >and I wish to remove (delete) the "," from it.


    The details depends on missing parts of the problem
    specification. For example, to delete the »,« at compile time,
    change the source code to

    basic::string myString = "123 E Main Street";

    . A function to delete the »,« at run-time depends on the
    range of permissible values for the specification of the
    string and the text to be deleted and the intended result or
    effect.

    I think the best way to pose such a question is to write a
    function declaration (but not a function definition) plus
    documentation for the function and then to ask for the
    implementation. It would even be better if a test client
    would be supplied by you, too.
    Stefan Ram, Apr 1, 2009
    #2
    1. Advertising

  3. Mike Copeland

    Tim Love Guest

    (Mike Copeland) writes:

    > What is a good way to remove a character from a basic::string?
    >For example, I have the following:


    > basic::string myString = "123 E, Main Street";


    >and I wish to remove (delete) the "," from it.
    >Does anyone know a way to do this? TIA

    I think the following will work (with a few includes, etc). Up to you whether you think it's a good way.


    string::iterator new_end =remove_if(myString.begin(), myString.end(), bind2nd(equal_to<char>(), ','));
    myString.erase(new_end, myString.end());
    Tim Love, Apr 1, 2009
    #3
  4. (Mike Copeland) writes:

    > What is a good way to remove a character from a basic::string?
    > For example, I have the following:
    >
    > basic::string myString = "123 E, Main Street";
    >
    > and I wish to remove (delete) the "," from it.
    > Does anyone know a way to do this? TIA


    I don't know basic::string. Perhaps you can convert them to std::string?
    Then you could do:

    {
    std::string temp=myString;
    std::string::iterator it=temp.find(',');
    if(it!=temp.end()){
    temp.erase(it);
    }
    myString=temp;
    }

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Apr 1, 2009
    #4
  5. On Apr 1, 10:41 am, (Mike Copeland) wrote:
    >    What is a good way to remove a character from a basic::string?  
    > For example, I have the following:
    >
    >         basic::string myString = "123 E, Main Street";
    >
    > and I wish to remove (delete) the "," from it.  
    > Does anyone know a way to do this?  TIA


    RTFM! http://www.sgi.com/tech/stl/basic_string.html
    Vladyslav Lazarenko, Apr 1, 2009
    #5

  6. >
    > string::iterator new_end =remove_if(myString.begin(), myString.end(),
    > bind2nd(equal_to<char>(), ','));
    > myString.erase(new_end, myString.end());


    Awesome! Thanks a lot for sharing, I never stumbled upon bind2nd() before.
    Gernot Frisch, Apr 1, 2009
    #6
  7. Mike Copeland

    James Kanze Guest

    On Apr 1, 4:51 pm, (Tim Love) wrote:
    > (Mike Copeland) writes:
    > > What is a good way to remove a character from a basic::string?
    > >For example, I have the following:
    > > basic::string myString = "123 E, Main Street";
    > >and I wish to remove (delete) the "," from it.
    > >Does anyone know a way to do this? TIA


    > I think the following will work (with a few includes, etc). Up
    > to you whether you think it's a good way.


    > string::iterator new_end =remove_if(myString.begin(), myString.end(), bind2nd(equal_to<char>(), ','));
    > myString.erase(new_end, myString.end());


    Isn't that doing things the hard way? There are times when
    bind2nd and such are necessary, but the above does exactly the
    same thing as:

    myString.erase(
    std::remove( myString.begin(), myString.end(), ',' ),
    myString.end() ) ;

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 1, 2009
    #7
  8. Mike Copeland

    James Kanze Guest

    On Apr 1, 4:54 pm, (Pascal J. Bourguignon)
    wrote:
    > (Mike Copeland) writes:
    > > What is a good way to remove a character from a basic::string?
    > > For example, I have the following:


    > > basic::string myString = "123 E, Main Street";


    > > and I wish to remove (delete) the "," from it.
    > > Does anyone know a way to do this? TIA


    > I don't know basic::string. Perhaps you can convert them to
    > std::string? Then you could do:


    > {
    > std::string temp=myString;
    > std::string::iterator it=temp.find(',');
    > if(it!=temp.end()){
    > temp.erase(it);
    > }
    > myString=temp;
    > }


    Which raises an interesting question: is temp.erase(it)
    guaranteed to work (i.e. do nothing) when it == temp.end()? I
    would expect it to, but I can't figure out whether it is
    guaranteed or not from the text in the standard.

    (There are also a couple of questions concerning what he really
    wants to do, of course. Remove the first comma? Remove all
    commas?, remove some specific character that he has found by
    other means?)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 1, 2009
    #8
  9. > > > basic::string myString = "123 E, Main Street";
    > > > and I wish to remove (delete) the "," from it.
    > > > Does anyone know a way to do this? TIA

    > > I don't know basic::string. Perhaps you can convert them to
    > > std::string? Then you could do:
    > > {
    > > std::string temp=3DmyString;
    > > std::string::iterator it = temp.find(',');
    > > if(it!=3Dtemp.end()){
    > > temp.erase(it);
    > > }
    > > myString=3Dtemp;
    > > }

    >
    > Which raises an interesting question: is temp.erase(it)
    > guaranteed to work (i.e. do nothing) when it == temp.end()? I
    > would expect it to, but I can't figure out whether it is
    > guaranteed or not from the text in the standard.
    >
    > (There are also a couple of questions concerning what he really
    > wants to do, of course. Remove the first comma? Remove all
    > commas?, remove some specific character that he has found by
    > other means?)


    FWIW, I want to remove/delete _all_ the commas in the string. My
    example didn't go into that, because I wasn't aware of any STL function
    that would delete a specific character from a string of characters. The
    myString.erase() function accomplishes what I need (but didn't know
    about), and I was able to modify the code to process multiples. I
    tested it with most pathological situations (e.g. comma in first and
    last positions), and it seems to do what I need.
    Thanks to everyone who contributed!
    Mike Copeland, Apr 2, 2009
    #9
    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. =?Utf-8?B?SklNLkgu?=

    Q: remove a certain string in string

    =?Utf-8?B?SklNLkgu?=, Feb 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    333
    Joshua Flanagan
    Feb 28, 2005
  2. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,345
    Henrik
    Aug 10, 2005
  3. Engineer
    Replies:
    6
    Views:
    603
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    417
  5. tshad
    Replies:
    6
    Views:
    21,435
    tshad
    Aug 8, 2006
Loading...

Share This Page