473,545 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3593
On 27 ÄÅË, 13:59, Andrew Wingorodov <m...@andr.cawr ote:
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.cawr ote:
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<po inter_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******@earth link.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::si ze_type First = text.find_first _not_of(TrimCha r);
if ( First == std::string::np os )
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*******@rocke tmail.com
Dec 28 '07 #5
"Jim Langston" <ta*******@rock etmail.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::si ze_type First = text.find_first _not_of(TrimCha r);
if ( First == std::string::np os )
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
89903
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? There is a global modifier to ignore all spaces in the pattern, but I couldn't find one for ignoring spaces in the subject string. Do I really have...
4
11183
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 Rocquencourt Tel : 01 39 63 56 27 --------------------------
1
4054
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 spaces thanks
6
22861
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
7841
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 i need to remove the + 0427 221 529 i need to remove both spaces 0419 637 344MNET i need to remove the spaces...
34
4061
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
15600
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 output string. But this was trivial. The other option is to use pointers and shift all the characters after the space by one space to the left. I did...
10
3176
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 remove the punctuation characters from the "first name" substring. I've looked at the basic_string in C++, but I can't find the functions that will...
26
13768
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 and return "12384" What is the most efficient, fastest way to approach this? Thanks,
0
7499
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7689
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.