CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Trimming of..

    Hi,

    I have a string object,I want to trim spaces from the left & right of the string.Is there any Inbuilt functions for this.The function should be operating system independent.

    Thanks in advance...

    [Yves : Your title was the beginning of a self-descriptive, good title, so please write it out in full ]
    Last edited by Yves M; November 29th, 2002 at 10:04 AM.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    As far as I know there isn't. Here's what I use, though:
    Code:
    string util::trim(const string& s, const string& toTrim /*= " \t\n"*/) 
    {
       return (string(rtrim(ltrim(s, toTrim), toTrim)));
    }
    
    string util::ltrim(const string& s, const string& toTrim /*= " \t\n"*/)
    {
       if (s.length() == 0)
       {
          return (s);
       }
    
       string::size_type index = s.find_first_not_of(toTrim);
       if (index == string::npos)
       {
          return ("");
       }
    
       return (string(s, index, s.end() - s.begin()));
    }
    
    string util::rtrim(const string& s, const string& toTrim /*= " \t\n"*/) 
    {
       if (s.length() == 0)
       {
          return (s);
       }
    
       string::size_type index = s.find_last_not_of(toTrim);
       return (string(s, 0, index+1));
    }
    There are plenty of algorithms that could accomplish what you
    want, though, I'm sure. It might look a little complex though?
    With the interface [and code appearance] my boss wanted,
    though, these functions work for me.

    Edit: Oops, I just noticed these are in a namespace. Just take
    off the util:: to make them global functions or make them static
    functions of some class or put them in a namespace of your own.
    The whole point of this edit is that it probably won't compile
    out-of-the-box with the util:: on there

    --Paul

  3. #3
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    i know no builtin function but...

    Hi,
    ok I know no builtin function that does the whole task, but basic_string / string offers a set of functions to do this easy AND compatible. you can port this to any ANSI-C++-environment . I will describe this not with a code sample, because I'm too tired now and don't want t post something with a wrong sytax etc.

    1. get an iterator for your string
    2. iterate throgh the string characters.
    3 test each character for whitespace / blank
    4. if it is a space-character call the remove() function of basic_string on the iterator.

    DONE !!!

    the remove()-function removes the character the iterator is pointing at and collapses the following characters to really remove the character.

    I hope you got the message, even though my english is terrible when I'm tired

    code on

    Juergen

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    He's kind of looking for something that trims off of the ends, not
    from the entire string.

    --Paul

  5. #5
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    ok

    hi paul,
    hmm my solution is easyly changeable to do this too:

    iterator to start of string -> do the things I described until you find something other than whitespace.

    iterator to end of string -> iterate backwards until you find something other than whitespace

    right ?

    Juergen

  6. #6
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Yes, but it wasn't accurate the way it was typed.

  7. #7
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    yess

    you are absolutely right... my fault

  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Hey there's no need for apologies, man. There's often more than
    one way to cook a duck or so the saying goes ... or is that skin a
    cat? Either way, the point it's good to see multiple ways to attack
    the same problem.

    --Paul

  9. #9
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    yeppp

    Hi Paul,
    that's what we are here for: Sharing solutions to avoid the trouble of inventing the wheel each time we want to use one. This gives us more brainspace for "exploring new territories".
    If the spirit of this forum would be in the whole world, in each community, I think we would have much less trouble in the world.

    keep the spirit

    Juergen

  10. #10
    Join Date
    Oct 2001
    Posts
    745
    Thanks..

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