remove spaces from begin a string



 DEVELOP > c-Plus-Plus > remove spaces from begin a string

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Andrew Wingorodov"
Date: 27 Dec 2007 04:59:53 AM
Object: remove spaces from begin a string
im made subj like this:
inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }
str.erase (
std.begin ()
, std::find_if ( str.begin(), str.end(), isnt_space)
);
How i can use std for better away?
Can i invert result of the ::isspace by means of bind2nd?
--
www.andr.mobi
.

User: "Daniel T."

Title: Re: remove spaces from begin a string 27 Dec 2007 11:02:06 AM
Andrew Wingorodov <mail@andr.ca> wrote:

im made subj like this:

inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }

The above could have been written more compactly as:
inline bool isnt_space( int sp ) { return !::isspace( sp ); }

str.erase (
std.begin ()
, std::find_if ( str.begin(), str.end(), isnt_space)
);

How i can use std for better away?
Can i invert result of the ::isspace by means of bind2nd?

Try this:
str.erase( str.begin(), find_if( str.begin(), str.end(),
not1( ptr_fun( &::isspace ) ) ) );
ptr_fun turns the function into an adaptable unary function
(http://www.sgi.com/tech/stl/AdaptableUnaryFunction.html)
not1 creates a unary_negate object, which inverts the result of the
function passed to it.
(http://www.sgi.com/tech/stl/unary_negate.html)
There may be a question as to whether what I provided is a "better way"
than what you did.
Of course you could always do both...
const unary_negate<pointer_to_unary_function<int, int> >
isnt_space = not1( ptr_fun( &::isspace ) );
str.erase( str.begin(),
find_if( str.begin(), str.end(), isnt_space ) );
.
User: "Andrew Wingorodov"

Title: Re: remove spaces from begin a string 28 Dec 2007 01:55:16 AM
Daniel T. <daniel_t@earthlink.net> wrote:

bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }


inline bool isnt_space( int sp ) { return !::isspace( sp ); }

OK

Try this:

str.erase( str.begin(), find_if( str.begin(), str.end(),
not1( ptr_fun( &::isspace ) ) ) );

str.erase( str.begin(),
find_if( str.begin(), str.end(), isnt_space ) );

10x
--
www.andr.mobi
.


User: "Jim Langston"

Title: Re: remove spaces from begin a string 28 Dec 2007 10:17:37 AM
Andrew Wingorodov wrote:

im made subj like this:

inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }

str.erase (
std.begin ()
, std::find_if ( str.begin(), str.end(), isnt_space)
);

How i can use std for better away?
Can i invert result of the ::isspace by means of bind2nd?

This is what I use to trim spaces from the beginning and end of a
std::string:
std::string trim( const std::string& text, const char TrimChar = ' ' );
std::string trim( const std::string& text, const char TrimChar )
{
std::string::size_type First = text.find_first_not_of(TrimChar);
if ( First == std::string::npos )
return "";
return text.substr( First, text.find_last_not_of(TrimChar) - First +
1);
}
This will remove the trim char (defaults to space, but you can make it
anything) from the beginning and end of a string.
--
Jim Langston
tazmaster@rocketmail.com
.
User: "Daniel T."

Title: Re: remove spaces from begin a string 28 Dec 2007 12:40:16 PM
"Jim Langston" <tazmaster@rocketmail.com> wrote:

Andrew Wingorodov wrote:

inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }

str.erase (
std.begin ()
, std::find_if ( str.begin(), str.end(), isnt_space)
);

How i can use std for better away?
Can i invert result of the ::isspace by means of bind2nd?


This is what I use to trim spaces from the beginning and end of a
std::string:

std::string trim( const std::string& text, const char TrimChar = ' ' );

std::string trim( const std::string& text, const char TrimChar )
{
std::string::size_type First = text.find_first_not_of(TrimChar);
if ( First == std::string::npos )
return "";
return text.substr( First, text.find_last_not_of(TrimChar) - First +
1);
}

This will remove the trim char (defaults to space, but you can make it
anything) from the beginning and end of a string.

Note, isspace returns true for 6 different characters...
.


User: "Pavel Shved"

Title: Re: remove spaces from begin a string 27 Dec 2007 05:29:08 AM
On 27 =C4=C5=CB, 13:59, Andrew Wingorodov <m...@andr.ca> wrote:

im made subj like this:

inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }

str.erase (
=9A =9A =9A =9A =9A std.begin ()
=9A =9A =9A =9A , std::find_if ( str.begin(), str.end(), isnt_space)
);

How i can use std for better away?

You can read manual on STL. Check something like `not1' (thats a
`one' which means `negate unary predicate').
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER