473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Dec 27 '07 #1
5 3572
On 27 ΔΕΛ, 13:59, Andrew Wingorodov <m...@andr.cawrote:
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?
You can read manual on STL. Check something like `not1' (thats a
`one' which means `negate unary predicate').

Dec 27 '07 #2
Andrew Wingorodov <ma**@andr.cawrote:
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 ) );
Dec 27 '07 #3
Daniel T. <da******@earthlink.netwrote:
>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
Dec 28 '07 #4
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
ta*******@rocketmail.com
Dec 28 '07 #5
"Jim Langston" <ta*******@rocketmail.comwrote:
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...
Dec 28 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Thomas Mlynarczyk | last post by:
Which is the simplest way to remove all whitespace from a string? Is there a simpler method than a regex replace? Or how can I tell a regex pattern to ignore all whitespace in my subject string?...
4
by: Marc Durufle | last post by:
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...
1
by: Danny | last post by:
Given this: "*" the pattern to remove the htnl code between brackets, how can I remove a series of spaces and just make one space. like here is a series of blank...
6
by: Paul | last post by:
Hi just wondering if there is an easy way to remove spaces in a string, tried the trim method but think it is only for leading or trailing spaces. thanks -- Paul G Software engineer.
3
by: Danny Yeadon | last post by:
Hi I need to remove unwanted characters from phone numbers from my phone bill to analyse the data. Some examples are 02 48222222 i need to remove the space +61266667656 ...
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
10
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.