Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What is the C++ idiom to strip leading and lagging white space from astring?

Reply
Thread Tools

What is the C++ idiom to strip leading and lagging white space from astring?

 
 
Ramon F Herrera
Guest
Posts: n/a
 
      09-08-2009

I have done blank stripping a million times, in C and Perl.

Please bear with me as I am trying to pick up the C++ "way of doing
things".

Thx,

-RFH

 
Reply With Quote
 
 
 
 
Francesco
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 4:39 pm, Ramon F Herrera <(E-Mail Removed)> wrote:
> I have done blank stripping a million times, in C and Perl.
>
> Please bear with me as I am trying to pick up the C++ "way of doing
> things".
>
> Thx,


Just a clarification, what do you mean with "lagging"? Do you mean
"trailing"? Or you do mean multiple whitespace occurrences between
words, i.e "just like this" to become "just like this"?

STL streams have a manipulator that skips whitespace, make a search
for "skipws".

I could easily write a function for doing all the above, I believe you
can do the same, pretty like as you have done in C.

You could also use some string functions (take a look to
http://www.cppreference.com/wiki/str...d_first_not_of) to
implement such a trim_lead_double_trail_ws() function.

Best regards,
Francesco
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-08-2009
Ramon F Herrera wrote:
> I have done blank stripping a million times, in C and Perl.
>
> Please bear with me as I am trying to pick up the C++ "way of doing
> things".


Not sure about the idiom, but

char const WS[] = "..."; // whatever you consider WS
str.erase(0, str.find_first_not_of(WS));
str.erase(str.find_last_not_of(WS) + 1);

should do it. If you want your WS to be determined by a function, like
'isspace' or whatnot, you need to write a functor and remove/erase those
for which the functor is true or vice versa, keep those for which the
functor yields false... A bit more convoluted.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Lucius Sanctimonious
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 10:57*am, Francesco <(E-Mail Removed)> wrote:
> On Sep 8, 4:39 pm, Ramon F Herrera <(E-Mail Removed)> wrote:
>
> > I have done blank stripping a million times, in C and Perl.

>
> > Please bear with me as I am trying to pick up the C++ "way of doing
> > things".

>
> > Thx,

>


> Just a clarification, what do you mean with
> "lagging"? Do you mean "trailing"?


Exactly.

I realize that I could write a function, but the reason I am moving
from C to C++ is because there are so many of those functions and
classes already.

-Ramon

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-08-2009
Lucius Sanctimonious wrote:
> On Sep 8, 10:57 am, Francesco <(E-Mail Removed)> wrote:
>> On Sep 8, 4:39 pm, Ramon F Herrera <(E-Mail Removed)> wrote:
>>
>>> I have done blank stripping a million times, in C and Perl.
>>> Please bear with me as I am trying to pick up the C++ "way of doing
>>> things".
>>> Thx,

>
> > Just a clarification, what do you mean with
> > "lagging"? Do you mean "trailing"?

>
> Exactly.
>
> I realize that I could write a function, but the reason I am moving
> from C to C++ is because there are so many of those functions and
> classes already.


Just so you don't get disappointed on your way from C to C++, most of
the code C++ programmers write is *functions*. Just happens to be that
way. The solution I proposed isn't a single function call and it would
be better wrapped in a function (or even perhaps a function template
<gasp!>).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Francesco
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 5:13*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> Lucius Sanctimonious wrote:
> > On Sep 8, 10:57 am, Francesco <(E-Mail Removed)> wrote:
> >> On Sep 8, 4:39 pm, Ramon F Herrera <(E-Mail Removed)> wrote:

>
> >>> I have done blank stripping a million times, in C and Perl.
> >>> Please bear with me as I am trying to pick up the C++ "way of doing
> >>> things".
> >>> Thx,

>
> > *> Just a clarification, what do you mean with
> > *> "lagging"? Do you mean "trailing"?

>
> > Exactly.

>
> > I realize that I could write a function, but the reason I am moving
> > from C to C++ is because there are so many of those functions and
> > classes already.

>
> Just so you don't get disappointed on your way from C to C++, most of
> the code C++ programmers write is *functions*. *Just happens to be that
> way. *The solution I proposed isn't a single function call and it would
> be better wrapped in a function (or even perhaps a function template
> <gasp!>).


Heck, I can see Victor's reply to Lucius' (???) reply to my reply to
Ramon's post, but I cannot see Lucius (???) reply! Damn, this NNTP
thing is driving me crazy =/

Anyway, this is one of those many functions that Victor spoke about:

-------
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string trim_ws(const string& s) {
size_t begin = s.find_first_not_of(' ');
if (begin == string::npos) {
return "";
}
size_t end = s.find_last_not_of(' ') + 1;
stringstream ss;
bool skipit = false;
for (size_t i = begin; i < end; ++i) {
char ch = s[i];
if (ch != ' ') {
ss << ch;
skipit = false;
} else if (!skipit) {
ss << ch;
skipit = true;
}
}
return ss.str();
}

int main()
{
string s = " just like this ";
cout << "[" << s << "]" << endl;
cout << "[" << trim_ws(s) << "]" << endl;
return 0;
}
-------

Just wrote it on the fly for exercise.
You could easily extend it with the "whatever character" idea
mentioned by Victor.

Cheers,
Francesco
 
Reply With Quote
 
Ramon F Herrera
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 11:32*am, Francesco <(E-Mail Removed)> wrote:
> On Sep 8, 5:13*pm, Victor Bazarov <(E-Mail Removed)> wrote:
>
>
>
> > Lucius Sanctimonious wrote:
> > > On Sep 8, 10:57 am, Francesco <(E-Mail Removed)> wrote:
> > >> On Sep 8, 4:39 pm, Ramon F Herrera <(E-Mail Removed)> wrote:

>
> > >>> I have done blank stripping a million times, in C and Perl.
> > >>> Please bear with me as I am trying to pick up the C++ "way of doing
> > >>> things".
> > >>> Thx,

>
> > > *> Just a clarification, what do you mean with
> > > *> "lagging"? Do you mean "trailing"?

>
> > > Exactly.

>
> > > I realize that I could write a function, but the reason I am moving
> > > from C to C++ is because there are so many of those functions and
> > > classes already.

>
> > Just so you don't get disappointed on your way from C to C++, most of
> > the code C++ programmers write is *functions*. *Just happens to be that
> > way. *The solution I proposed isn't a single function call and it would
> > be better wrapped in a function (or even perhaps a function template
> > <gasp!>).

>



> Heck, I can see Victor's reply to Lucius' (???)
> reply to my reply to Ramon's post, but I cannot see
> Lucius (???) reply! Damn, this NNTP
> thing is driving me crazy =/


It is my fault, I posted from a co-worker's account which should be
only used for work, so I removed it.

-Ramon

 
Reply With Quote
 
Ramon F Herrera
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 11:39*am, Markus Raab <(E-Mail Removed)> wrote:
> Ramon F Herrera wrote:
> > I have done blank stripping a million times, in C and Perl.

>
> > Please bear with me as I am trying to pick up the C++ "way of doing
> > things".

>
> boost::trim
>
> http://www.boost.org/doc/libs/1_37_0...tring/trim.hpp
>
> Markus
>
> --http://www.markus-raab.org| Probleme kann man nie mit derselben
> * * * * * * * * * * * -o) *| Denkweise lösen, durch die sie entstanden
> Kernel 2.6.24-1-a * * */\ *| sind. *-- Albert Einstein *
> on a x86_64 * * * * * _\_v |



Thanks, Markus! That is exactly what I had in mind when I posted the
question. I happen to be a recent boost convert. Downloaded and
installed it yesterday.

Just to make sure, is this the correct way to add the include?

#include <boost/algorithm/string/trim.hpp>

Regards,

-Ramon

 
Reply With Quote
 
Ramon F Herrera
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 10:57*am, Francesco <(E-Mail Removed)> wrote:
> On Sep 8, 4:39 pm, Ramon F Herrera <(E-Mail Removed)> wrote:
>
> > I have done blank stripping a million times, in C and Perl.

>
> > Please bear with me as I am trying to pick up the C++ "way of doing
> > things".

>
> > Thx,

>


> Just a clarification, what do you mean with "lagging"?
> Do you mean "trailing"?


Blame it on my Economics classes, peppered with "leading indicators"
and "lagging indicators". Those are used to predict recessions and
exit from them. Very pertinent stuff these days!

-Ramon

 
Reply With Quote
 
Francesco
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 5:48*pm, Ramon F Herrera <(E-Mail Removed)> wrote:
> On Sep 8, 11:32*am, Francesco <(E-Mail Removed)> wrote:
>
>
>
> > On Sep 8, 5:13*pm, Victor Bazarov <(E-Mail Removed)> wrote:

>
> > > Lucius Sanctimonious wrote:
> > > > On Sep 8, 10:57 am, Francesco <(E-Mail Removed)> wrote:
> > > >> On Sep 8, 4:39 pm, Ramon F Herrera <(E-Mail Removed)> wrote:

>
> > > >>> I have done blank stripping a million times, in C and Perl.
> > > >>> Please bear with me as I am trying to pick up the C++ "way of doing
> > > >>> things".
> > > >>> Thx,

>
> > > > *> Just a clarification, what do you mean with
> > > > *> "lagging"? Do you mean "trailing"?

>
> > > > Exactly.

>
> > > > I realize that I could write a function, but the reason I am moving
> > > > from C to C++ is because there are so many of those functions and
> > > > classes already.

>
> > > Just so you don't get disappointed on your way from C to C++, most of
> > > the code C++ programmers write is *functions*. *Just happens to be that
> > > way. *The solution I proposed isn't a single function call and it would
> > > be better wrapped in a function (or even perhaps a function template
> > > <gasp!>).

>
> *> Heck, I can see Victor's reply to Lucius' (???)
> *> reply to my reply to Ramon's post, but I cannot see
> *> Lucius (???) reply! Damn, this NNTP
> *> thing is driving me crazy =/
>
> It is my fault, I posted from a co-worker's account which should be
> only used for work, so I removed it.


Fine, no problem. I also thought that Google Groups went mad at some
point =>

Francesco
 
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
strip all but second second line from bottom and then strip that!!!! yelipolok Perl Misc 4 01-27-2010 08:14 AM
Any programs to trim white space/ remove all white space in HTML file? Ben C HTML 6 01-28-2007 11:41 PM
Q: strip white space Malcolm Dew-Jones XML 2 04-13-2005 12:03 AM
strip and its evil brother strip! Aquila Ruby 35 03-31-2005 04:10 AM
How does union datatype handle leading/trailing white space? Leo XML 0 08-31-2004 03:26 PM



Advertisments