By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

clear spaces in a string

P: n/a
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
--------------------------
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Marc Durufle" <Ma**********@inria.fr> wrote in message
news:3F***************@inria.fr...
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
Jul 19 '05 #2

P: n/a

Marc Durufle <Ma**********@inria.fr> wrote in message
news:3F***************@inria.fr...
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


Jul 19 '05 #3

P: n/a
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
rh*******@pobox.com

Jul 19 '05 #4

P: n/a
"Marc Durufle" <Ma**********@inria.fr> wrote in message
news:3F***************@inria.fr...
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

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.