remove trailing whitespace from string

Discussion in 'C++' started by Donald Canton, Feb 9, 2004.

  1. What's the best way to remove trailing whitespace from a string? For
    example: "John C. Doe \t" would become "John C. Doe". Thanks.
     
    Donald Canton, Feb 9, 2004
    #1
    1. Advertising

  2. Donald Canton wrote:
    >
    > What's the best way to remove trailing whitespace from a string? For
    > example: "John C. Doe \t" would become "John C. Doe". Thanks.


    By gooing to www.google.com, switching to the newsgroup archives
    and doing some research.
    A search string of "C++ trailing whitespace eliminate" comes
    up with a lot of good hits.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Feb 9, 2004
    #2
    1. Advertising

  3. "Donald Canton" <> wrote...
    > What's the best way to remove trailing whitespace from a string? For
    > example: "John C. Doe \t" would become "John C. Doe". Thanks.


    Something like

    string::reverse_iterator it = somestring.rbegin();
    while (it != somestring.rend() && isspace(*it))
    {
    somestring.erase(it);
    it = somestring.rbegin();
    }

    That's if we're talking of 'string' type. For char array
    you do

    int lci = strlen(somestring) - 1;
    while (lci >= 0 && isspace(somestring[lci])
    somestring[lci--] = 0; // move the terminator there

    Disclaimer: code wasn't tested.

    V
     
    Victor Bazarov, Feb 9, 2004
    #3
  4. "Donald Canton" <> wrote in message
    news:...
    > What's the best way to remove trailing whitespace from a string? For
    > example: "John C. Doe \t" would become "John C. Doe". Thanks.


    This is untested code

    const char* const whitespace = " \t\r\n\v\f";

    s.erase(s.find_last_not_of(whitespace) + 1);

    john
     
    John Harrison, Feb 9, 2004
    #4
  5. Donald Canton wrote:

    > What's the best way to remove trailing whitespace from a string? For
    > example: "John C. Doe \t" would become "John C. Doe".


    I used Google

    http://www.google.com/

    to search for

    +"C++" +"trim string"

    and I found lots of stuff.
     
    E. Robert Tisdale, Feb 9, 2004
    #5
  6. Hello Donald,

    > What's the best way to remove trailing whitespace from a string? For
    > example: "John C. Doe \t" would become "John C. Doe". Thanks.


    If you have to deal with different locales and/or wide-character
    strings, the following code might be useful:

    template<class E, class T, class A> class C_IsNotSpace_
    {
    private:
    const std::locale& loc;
    public:
    C_IsNotSpace_(const std::locale& l) : loc(l) { }
    inline bool operator()(E ch) const { return !std::isspace(ch, loc); }
    };

    template<class E, class T, class A>
    inline std::basic_string<E, T, A> TrimRight(
    const std::basic_string<E, T, A>& str,
    const std::locale& loc = std::locale())
    {
    using namespace std;
    typedef basic_string<E, T, A> T_Str;
    C_IsNotSpace_<E, T, A> isNotSpace(loc);

    typename T_Str::const_reverse_iterator rb = str.rbegin();
    typename T_Str::const_reverse_iterator re = str.rend();
    typename T_Str::const_reverse_iterator last = find_if(rb, re,
    isNotSpace);

    return (last == re) ? T_Str() : T_Str(re.base(), last.base());
    }

    // You can use it like this:

    string s1 = " My test string \n";
    string s2 = TrimRight(s1);

    Tilman
     
    Tilman Kuepper, Feb 9, 2004
    #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. Brad Marts
    Replies:
    3
    Views:
    424
    Brad Marts
    Nov 17, 2003
  2. KevinGPO
    Replies:
    1
    Views:
    350
    Richard Herring
    Feb 20, 2006
  3. Dotan Cohen
    Replies:
    2
    Views:
    523
    Dotan Cohen
    Oct 10, 2011
  4. Bob Smyph

    Remove only TRAILING whitespace

    Bob Smyph, Oct 14, 2008, in forum: Ruby
    Replies:
    4
    Views:
    162
    Bob Smyph
    Oct 14, 2008
  5. Replies:
    3
    Views:
    196
Loading...

Share This Page