trim string.

Discussion in 'C++' started by DrBob, Nov 26, 2003.

  1. DrBob

    DrBob Guest

    gcc 3.3 MAC OS X.

    I have a string that has trailing spaces in it that I want removed.
    So i have a variable:
    string x("abcd ");

    x.trim() isn't an implemented method.
    Is there a method I don't know about?

    What do I do to extent the string class such that there is a method to
    trim the trailing spaces... (Assuming a method doesn't exist that I'm
    not aware of.)
    DrBob, Nov 26, 2003
    #1
    1. Advertising

  2. DrBob

    Unforgiven Guest

    DrBob wrote:
    > gcc 3.3 MAC OS X.
    >
    > I have a string that has trailing spaces in it that I want removed.
    > So i have a variable:
    > string x("abcd ");
    >
    > x.trim() isn't an implemented method.
    > Is there a method I don't know about?
    >
    > What do I do to extent the string class such that there is a method to
    > trim the trailing spaces... (Assuming a method doesn't exist that I'm
    > not aware of.)


    Use find_first_not_of and find_last_not_of to find the positions of the
    first and last non-whitespace characters, then use substr to get only the
    part of the string you need.

    You should've been able to figure that one out yourself really.

    --
    Unforgiven

    "You can't rightfully be a scientist if you mind people thinking
    you're a fool."
    Unforgiven, Nov 26, 2003
    #2
    1. Advertising

  3. DrBob

    Jonathan Guest

    "DrBob" <> wrote in message
    news:...
    > gcc 3.3 MAC OS X.
    >
    > I have a string that has trailing spaces in it that I want removed.
    > So i have a variable:
    > string x("abcd ");
    >
    > x.trim() isn't an implemented method.
    > Is there a method I don't know about?
    >
    > What do I do to extent the string class such that there is a method to
    > trim the trailing spaces... (Assuming a method doesn't exist that I'm
    > not aware of.)


    I am not sure about extending the class, but if the first part of your
    string does not contain spaces, you could always just look up the index of
    the first space, and cut the end off.

    string x("abcd ");
    string y(x.begin, x.find(' '));

    jonathan
    Jonathan, Nov 26, 2003
    #3
  4. DrBob

    John Carson Guest

    "Jonathan" <> wrote in message
    news:
    > "DrBob" <> wrote in message
    > news:...
    > > gcc 3.3 MAC OS X.
    > >
    > > I have a string that has trailing spaces in it that I want removed.
    > > So i have a variable:
    > > string x("abcd ");
    > >
    > > x.trim() isn't an implemented method.
    > > Is there a method I don't know about?
    > >
    > > What do I do to extent the string class such that there is a method
    > > to trim the trailing spaces... (Assuming a method doesn't exist
    > > that I'm not aware of.)

    >
    > I am not sure about extending the class, but if the first part of your
    > string does not contain spaces, you could always just look up the
    > index of the first space, and cut the end off.
    >
    > string x("abcd ");
    > string y(x.begin, x.find(' '));
    >
    > jonathan


    And if you want to modify the original string rather than create a new one:

    string x("abcd ");
    string::size_type st = x.find(' ');
    x.erase(st, x.length()-st);


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, Nov 27, 2003
    #4
  5. DrBob

    John Carson Guest

    "John Carson" <> wrote in message
    news:3fc54c80$

    > And if you want to modify the original string rather than create a
    > new one:
    >
    > string x("abcd ");
    > string::size_type st = x.find(' ');
    > x.erase(st, x.length()-st);
    >


    Actually, you only need:

    string x("abcd ");
    x.erase(x.find(' '));


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, Nov 27, 2003
    #5
  6. DrBob

    Jon Bell Guest

    In article <3fc55b66$>,
    John Carson <> wrote:
    >
    > string x("abcd ");
    > x.erase(x.find(' '));


    Of course, that won't work if the string has embedded spaces. How about
    searching from the end?

    x.erase(x.find_last_not_of(' ')+1);

    Or if we want to trim any whitespace (not just blanks):

    x.erase(x.find_last_not_of(" \t\n")+1);

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Nov 27, 2003
    #6
  7. "Jon Bell" <> wrote in message
    news:bq416g$jb5$...
    | In article <3fc55b66$>,
    | John Carson <> wrote:
    | >
    | > string x("abcd ");
    | > x.erase(x.find(' '));
    |
    | Of course, that won't work if the string has embedded spaces. How about
    | searching from the end?
    |
    | x.erase(x.find_last_not_of(' ')+1);
    |
    | Or if we want to trim any whitespace (not just blanks):
    |
    | x.erase(x.find_last_not_of(" \t\n")+1);

    Caveat: npos+1 == ? (the npos return value needs to be tested for).
    I suspect both of these will fail if no blank is found ...


    Regards,
    Ivan
    --
    http://ivan.vecerina.com
    Ivan Vecerina, Nov 27, 2003
    #7
  8. "DrBob" <> wrote in message
    news:...
    | I have a string that has trailing spaces in it that I want removed.
    | So i have a variable:
    | string x("abcd ");
    |
    | x.trim() isn't an implemented method.
    | Is there a method I don't know about?
    |
    | What do I do to extent the string class such that there is a method to
    | trim the trailing spaces... (Assuming a method doesn't exist that I'm
    | not aware of.)

    No such method => you should implement a non-member function to do so.
    I use:


    char const kBlankChars[] = " \t\n\r";

    /// Returns a string with leading/trailing characters of a set stripped
    std::string trimmed
    ( std::string const& str ///< the original string
    , char const* sepSet=kBlankChars ///< chars to be dropped
    )
    {
    std::string::size_type const first = str.find_first_not_of(sepSet);
    return ( first==std::string::npos )
    ? std::string()
    : str.substr(first, str.find_last_not_of(sepSet)-first+1);
    }

    std::string rtrimmed( std::string const& str, char const* sepSet )
    {
    std::string::size_type const last = str.find_last_not_of(sepSet);
    return ( last==std::string::npos )
    ? std::string()
    : str.substr(0, last+1);
    }

    std::string ltrimmed( std::string const& str, char const* sepSet )
    {
    std::string::size_type const first = str.find_first_not_of(sepSet);
    return ( first==std::string::npos )
    ? std::string()
    : str.substr( first );
    }


    Let me know if you see any bug in this...


    Ivan
    --
    http://ivan.vecerina.com
    Ivan Vecerina, Nov 27, 2003
    #8
  9. "Ivan Vecerina" <> wrote in message
    news:bq4rtv$nnf$...
    | | Or if we want to trim any whitespace (not just blanks):
    | |
    | | x.erase(x.find_last_not_of(" \t\n")+1);
    |
    | Caveat: npos+1 == ? (the npos return value needs to be tested for).
    | I suspect both of these will fail if no blank is found ...

    Woops... no. I sent this too soon.
    In fact:
    npos is returned if the string only contains blanks.
    npos+1 == 0
    So it should be ok.

    My apologies,
    Ivan
    --
    http://ivan.vecerina.com
    Ivan Vecerina, Nov 27, 2003
    #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. FAQ server
    Replies:
    0
    Views:
    139
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    135
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    6
    Views:
    214
    Jonas Raoni
    Dec 25, 2006
  4. FAQ server
    Replies:
    26
    Views:
    295
    Dr J R Stockton
    Feb 26, 2007
  5. FAQ server
    Replies:
    2
    Views:
    130
    -Lost
    Apr 24, 2007
Loading...

Share This Page