CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2009
    Posts
    26

    Talking Removing characters from a string [Solved]

    Okay, what I basically want to do is to remove the two last characters from my string witch is a
    tab and a newline character, but I think that doesn't matter, because they are always at the end of
    the string. I got a code already, but it doesn't work correctly.

    Code:
        string::size_type pos = 0;
        while ((pos = txt.find_first_of("\n\t", pos) != string::npos))
            txt.erase(pos, 1);
    
        return txt;
    It seems like that code removes everything except the first character.
    Thanks in advance
    Last edited by WirelessDice; May 10th, 2009 at 07:36 AM. Reason: *solved*

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Removing characters from a string

    Hello WirelessDice

    check the while condition and see what value it returns on each iteration
    bye~

  3. #3
    Join Date
    Feb 2009
    Posts
    26

    Exclamation Re: Removing characters from a string

    Quote Originally Posted by potatoCode View Post
    Hello WirelessDice

    check the while condition and see what value it returns on each iteration
    bye~
    I edited the code so I could see the return values.

    Code:
        cout<<"TEST!!!!\n";
        string::size_type pos = 0;
        while ((pos = txt.find_first_of("\n", pos) != string::npos))
        {
            cout<<pos<<"\n";
            txt.erase(pos, 1);
        }
        cout<<"END TEST!!!\n";
    The return values I got was something like this.

    Code:
    TEST!!!
    1
    1
    1
    END TEST!!!
    And instead of getting "-1" I'll only get "-", pleas help!
    Thanks in advance.

  4. #4
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Removing characters from a string

    Quote Originally Posted by WirelessDice View Post
    I edited the code so I could see the return values.

    Code:
        cout<<"TEST!!!!\n";
        string::size_type pos = 0;
        while ((pos = txt.find_first_of("\n", pos) != string::npos))
        {
            cout<<pos<<"\n";
            txt.erase(pos, 1);
        }
        cout<<"END TEST!!!\n";
    The return values I got was something like this.

    Code:
    TEST!!!
    1
    1
    1
    END TEST!!!
    And instead of getting "-1" I'll only get "-", pleas help!
    Thanks in advance.
    There goes your answer~
    Since the return value is always 1,
    the erase() always starts at that index position!

  5. #5
    Join Date
    Feb 2009
    Posts
    26

    Lightbulb Re: Removing characters from a string

    Quote Originally Posted by potatoCode View Post
    There goes your answer~
    Since the return value is always 1,
    the erase() always starts at that index position!
    Well, okay.. So how do I fix it? Or else, just change the whole code to remove the two last
    characters!

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Removing characters from a string

    Quote Originally Posted by WirelessDice View Post
    Well, okay.. So how do I fix it? Or else, just change the whole code to remove the two last
    characters!
    That's a find solution if only the last two strings need to be removed unconditionally

    or do something along the line of
    Code:
        while(1)
        {
            pos = txt.find_first_of("\n\t");
            if(pos == string::npos)
                break;
            txt.erase(pos, 1);
        }
    xD

  7. #7
    Join Date
    Apr 2008
    Posts
    725

    Re: Removing characters from a string

    give *complete compilable* code. e.g. what is in txt ??

    also, why use find_FIRST_of, when you want to find the \t\n at the end of the string? just use find_last_of.

    most importantly:

    Code:
    pos = txt.find_first_of("\n", pos) != string::npos;
    is the same as
    Code:
    pos = ( txt.find_first_of("\n", pos) != string::npos );
    therefore, for what you have coded, pos will only ever be 1 (or whatever bool(true) gets cast to as a string::size_type) inside that while loop.


    try this:
    Code:
      while ( string::npos != (pos = txt.find_first_of("\n")) )
      {
        ...
      }
    Last edited by Amleto; May 10th, 2009 at 09:36 AM.

  8. #8
    Join Date
    Feb 2009
    Posts
    26

    Exclamation Re: Removing characters from a string

    Quote Originally Posted by potatoCode View Post
    That's a find solution if only the last two strings need to be removed unconditionally

    or do something along the line of
    Code:
        while(1)
        {
            pos = txt.find_first_of("\n\t");
            if(pos == string::npos)
                break;
            txt.erase(pos, 1);
        }
    xD
    Wow, Thanks! That actually worked!
    Thread solved!

  9. #9
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Removing characters from a string

    Quote Originally Posted by WirelessDice View Post
    Thread solved!
    You should prefer to use standard algorithms wherever possible. All of that crap can be replaced by a one-liner involving std::remove_if and std::isspace:
    Code:
    txt.erase(std::remove_if(txt.begin(), txt.end(), &std::isspace), txt.end());
    Boost's string algorithms library provides more elegant solutions, if you're interested.

  10. #10
    Join Date
    Apr 2008
    Posts
    725

    Re: Removing characters from a string

    Quote Originally Posted by Plasmator View Post
    You should prefer to use standard algorithms wherever possible. All of that crap can be replaced by a one-liner involving std::remove_if and std::isspace:
    Code:
    txt.erase(std::remove_if(txt.begin(), txt.end(), &std::isspace), txt.end());
    Boost's string algorithms library provides more elegant solutions, if you're interested.
    cant get it to compile
    Code:
    _FwdIt std::remove_if(_FwdIt,_FwdIt,_Pr)' : cannot use function template 'bool std::isspace(_Elem,const std::locale &)' as a function argument
    I included <locale>, not a c library though :s looks like that version isnt a predicate.

    I got it to work using isspace from #include <ctype.h> which isnt a member of std namespace. i probably shouldnt be including that file though?
    Last edited by Amleto; May 10th, 2009 at 11:52 AM.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Removing characters from a string

    Quote Originally Posted by Amleto
    I included <locale>, not a c library though :s looks like that version isnt a predicate.

    I got it to work using isspace from #include <ctype.h> which isnt a member of std namespace. i probably shouldnt be including that file though?
    The problem is that std::isspace is a function template. ::isspace from <ctype.h> is not a function template. If you want to stick with <cctype>, a simple solution is to implement your own function, possibly by overloading, that forwards the job to std::isspace.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Removing characters from a string

    Quote Originally Posted by Plasmator View Post
    You should prefer to use standard algorithms wherever possible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured