Removing the leading and tailing blanks

Discussion in 'C++' started by jalkadir, Oct 13, 2005.

  1. jalkadir

    jalkadir Guest

    I am trying to find a way to remove the leading and tailing blanks from
    a string.
    What I have come up with is not doing the work as I expected; because
    if the value I pass to the function is " string " the leading and
    trailing blanks are not removed. What am I doing wrong?

    trimIt( std::string s ) {
    size_t offset = s.length();
    // Trim leading spaces ???
    s.erase( 0, s.find_first_not_of( "\t\n" ) );
    // Trim trailing spaces ???
    s.erase( s.find_first_not_of( "\t\n" ) + offset );


    // This line is always displayed
    if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
    return s;
    }

    TIA
     
    jalkadir, Oct 13, 2005
    #1
    1. Advertising

  2. jalkadir

    Guest

    > trimIt( std::string s ) {

    First, try this...

    trimIt( std::string& s) {
     
    , Oct 13, 2005
    #2
    1. Advertising

  3. jalkadir

    ben Guest

    jalkadir wrote:
    > I am trying to find a way to remove the leading and tailing blanks from
    > a string.
    > What I have come up with is not doing the work as I expected; because
    > if the value I pass to the function is " string " the leading and
    > trailing blanks are not removed. What am I doing wrong?
    >
    > trimIt( std::string s ) {
    > size_t offset = s.length();
    > // Trim leading spaces ???
    > s.erase( 0, s.find_first_not_of( "\t\n" ) );
    > // Trim trailing spaces ???
    > s.erase( s.find_first_not_of( "\t\n" ) + offset );
    >
    >
    > // This line is always displayed
    > if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
    > return s;
    > }
    >
    > TIA
    >


    Use your debugger to step through the code. Take the temporaries out so
    you can examine their values.

    Ben
     
    ben, Oct 13, 2005
    #3
  4. jalkadir wrote:
    > I am trying to find a way to remove the leading and tailing blanks from
    > a string.
    > What I have come up with is not doing the work as I expected; because
    > if the value I pass to the function is " string " the leading and
    > trailing blanks are not removed. What am I doing wrong?
    >
    > trimIt( std::string s ) {
    > size_t offset = s.length();
    > // Trim leading spaces ???
    > s.erase( 0, s.find_first_not_of( "\t\n" ) );
    > // Trim trailing spaces ???
    > s.erase( s.find_first_not_of( "\t\n" ) + offset );
    >
    >
    > // This line is always displayed
    > if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
    > return s;
    > }
    >
    > TIA
    >


    A couple of things

    1) There is no space in "\t\n", you mean " \t\n".

    2) The call to trim trailing spaces is simply incorrect, it does nothing
    of the sort. It's more likely to crash your program than anything else.

    john
     
    John Harrison, Oct 13, 2005
    #4
  5. jalkadir

    jalkadir Guest

    Thanks John!
    I did as you suggested, but it causes a segmentation fault.
    I get a message saying: 'This application has requested the Runntime to
    terminate it in an unusual way.'
    I am using GCC-g++-3.4.2 on a MSWin-XP.

    I would like to know if you have a tangible example showing how to
    solve this problem; if so, could you share it?

    TIA
     
    jalkadir, Oct 13, 2005
    #5
  6. std::string trimIt( std::string s ) {
    size_t offset = s.length();
    // Trim leading spaces ???
    s.erase( 0, s.find_first_not_of( " \t\n" ) );
    // Trim trailing spaces ???
    s.erase( s.find_last_not_of( " \t\n" ) + 1);
    // This line is always displayed
    if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
    return s;
    }

    Does this work for you?
    Note: in the original post you wasn't passing the string by reference
    so i left it unchanged.
     
    paulius-maruska, Oct 13, 2005
    #6
  7. jalkadir

    jalkadir Guest

    Yes!!
    Thanks Paulius-maruska, it did the trick.
     
    jalkadir, Oct 15, 2005
    #7
    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. tfs
    Replies:
    1
    Views:
    409
    John Saunders
    Jun 28, 2004
  2. John Nagle
    Replies:
    4
    Views:
    615
    James Antill
    Aug 7, 2007
  3. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    582
    Delaney, Timothy (Tim)
    Aug 7, 2007
  4. PerlFAQ Server
    Replies:
    0
    Views:
    111
    PerlFAQ Server
    Feb 2, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    109
    PerlFAQ Server
    Mar 27, 2011
Loading...

Share This Page