Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > clear spaces in a string

Reply
Thread Tools

clear spaces in a string

 
 
Marc Durufle
Guest
Posts: n/a
 
      08-22-2003
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
--------------------------
 
Reply With Quote
 
 
 
 
Samuele Armondi
Guest
Posts: n/a
 
      08-22-2003
"Marc Durufle" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      08-22-2003

Marc Durufle <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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




 
Reply With Quote
 
Russell Hanneken
Guest
Posts: n/a
 
      08-22-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      08-23-2003
"Marc Durufle" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD to allow A-Z 0-9 and spaces, but not leading/trailing spaces johkar XML 2 12-10-2009 09:24 AM
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM



Advertisments