clear spaces in a string

Discussion in 'C++' started by Marc Durufle, Aug 22, 2003.

  1. Marc Durufle

    Marc Durufle Guest

    I have a string like that " Vertices "
    and i want to obtain "Vertices"
    I would want to know if there is a simple way to put off spaces on a
    string, thank you

    --
    Marc Durufle
    Inria Rocquencourt
    Tel : 01 39 63 56 27
    --------------------------
     
    Marc Durufle, Aug 22, 2003
    #1
    1. Advertising

  2. "Marc Durufle" <> wrote in message
    news:...
    > I have a string like that " Vertices "
    > and i want to obtain "Vertices"
    > I would want to know if there is a simple way to put off spaces on a
    > string, thank you
    >
    > --
    > Marc Durufle
    > Inria Rocquencourt
    > Tel : 01 39 63 56 27
    > --------------------------


    Try this:
    std::string s = " Vertices ";
    std::string::iterator it = std::remove_if(s.begin(), s.end(),
    std::bind2nd(std::equal_to<char>(), ' '));
    s = std::string(s.begin(), it);
    HTH,
    S. Armondi
     
    Samuele Armondi, Aug 22, 2003
    #2
    1. Advertising

  3. Marc Durufle

    Mike Wahler Guest

    Marc Durufle <> wrote in message
    news:...
    > I have a string like that " Vertices "
    > and i want to obtain "Vertices"
    > I would want to know if there is a simple way to put off spaces on a
    > string, thank you
    >
    > --
    > Marc Durufle
    > Inria Rocquencourt
    > Tel : 01 39 63 56 27
    > --------------------------


    #include <iostream>
    #include <sstream>
    #include <string>

    std::string strip(const std::string& s)
    {
    std::string result;
    std::istringstream(s) >> result;
    return result;
    }

    int main()
    {
    std::cout << '*'
    << strip(" Vertices ")
    << '*'
    << '\n';

    return 0;
    }

    -Mike
     
    Mike Wahler, Aug 22, 2003
    #3
  4. Mike Wahler wrote:
    >
    > std::istringstream(s) >> result;


    Mike,

    I don't think that will compile; you're passing a temporary by non-const
    reference. I believe you have to do something like this:

    std::istringstream iss(s);
    iss >> result;

    Regards,

    Russell Hanneken
     
    Russell Hanneken, Aug 22, 2003
    #4
  5. "Marc Durufle" <> wrote in message
    news:...
    > I have a string like that " Vertices "
    > and i want to obtain "Vertices"
    > I would want to know if there is a simple way to put off spaces on a
    > string, thank you


    Note: you should specify whether you intend to remove *all* spaces,
    or just trim the ones at the beginning and the end of the string.
    (several replies so far assume the former).

    Here's a way to remove spaces at both ends of the string:
    std::string trimmed( 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, str.find_last_not_of(sepSet)-first+1);
    }

    NB: - str is the original string
    - sepSet is the list of space characters.
    Use for example: " " or " \t" or " \t\n\r" etc.


    hth
    --
    Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Aug 23, 2003
    #5
    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. John B. Matthews
    Replies:
    4
    Views:
    670
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    626
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,606
    Jan C. Faerber
    Sep 10, 2009
  4. Tomasz Chmielewski

    how can I show spaces as spaces, part 2

    Tomasz Chmielewski, Sep 9, 2009, in forum: HTML
    Replies:
    14
    Views:
    877
    Jan C. Faerber
    Sep 10, 2009
  5. johkar
    Replies:
    2
    Views:
    2,902
    Mayeul
    Dec 10, 2009
Loading...

Share This Page