Re: Using Trim

Discussion in 'C++' started by Chris Theis, Sep 11, 2003.

  1. Chris Theis

    Chris Theis Guest

    "Jamil Anwar Zaman" <> wrote in message
    news:eek:...
    >
    > Hi folks,
    >
    > Can anyone give me a clue about how to use the trim() function. I need it
    > to remove the whitespace. I was googling for an example but couldn't find
    > any useful. And ofcourse, is it ansi standard ?
    > -- Using M2, Opera's revolutionary e-mail client:
    > http://www.opera.com/m2/


    There is no trim() function in standard C++. To remove all blanks from a
    string you can use this:

    ////////////////////////////////////////////////////////////////////////////
    //
    std::string StripBlanks( const std::string& Text )
    // remove all blanks from a string
    ////////////////////////////////////////////////////////////////////////////
    //
    {
    std::string Result = Text;
    std::string::iterator Iter = std::remove_if( Result.begin(), Result.end(),
    std::bind2nd(std::equal_to<char>(), ' '));
    return std::string( Result.begin(), Iter);
    }

    If you only want to remove beginning blanks you will have to modify the
    remove_if statement using the correct range. For this you'll have to find
    the first character which is not a blank. Just a little hint - the string
    class provides a find_first_not_of() function.

    HTH
    Chris
    Chris Theis, Sep 11, 2003
    #1
    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. FAQ server
    Replies:
    0
    Views:
    134
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    130
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    6
    Views:
    208
    Jonas Raoni
    Dec 25, 2006
  4. FAQ server
    Replies:
    26
    Views:
    289
    Dr J R Stockton
    Feb 26, 2007
  5. FAQ server
    Replies:
    2
    Views:
    127
    -Lost
    Apr 24, 2007
Loading...

Share This Page