By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,470 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

Removing the leading and tailing blanks

P: n/a
I am trying to find a way to remove the leading and tailing blanks from
a string.
What I have come up with is not doing the work as I expected; because
if the value I pass to the function is " string " the leading and
trailing blanks are not removed. What am I doing wrong?

trimIt( std::string s ) {
size_t offset = s.length();
// Trim leading spaces ???
s.erase( 0, s.find_first_not_of( "\t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_first_not_of( "\t\n" ) + offset );
// This line is always displayed
if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
return s;
}

TIA

Oct 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> trimIt( std::string s ) {

First, try this...

trimIt( std::string& s) {

Oct 13 '05 #2

P: n/a
ben
jalkadir wrote:
I am trying to find a way to remove the leading and tailing blanks from
a string.
What I have come up with is not doing the work as I expected; because
if the value I pass to the function is " string " the leading and
trailing blanks are not removed. What am I doing wrong?

trimIt( std::string s ) {
size_t offset = s.length();
// Trim leading spaces ???
s.erase( 0, s.find_first_not_of( "\t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_first_not_of( "\t\n" ) + offset );
// This line is always displayed
if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
return s;
}

TIA


Use your debugger to step through the code. Take the temporaries out so
you can examine their values.

Ben
Oct 13 '05 #3

P: n/a
jalkadir wrote:
I am trying to find a way to remove the leading and tailing blanks from
a string.
What I have come up with is not doing the work as I expected; because
if the value I pass to the function is " string " the leading and
trailing blanks are not removed. What am I doing wrong?

trimIt( std::string s ) {
size_t offset = s.length();
// Trim leading spaces ???
s.erase( 0, s.find_first_not_of( "\t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_first_not_of( "\t\n" ) + offset );
// This line is always displayed
if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
return s;
}

TIA


A couple of things

1) There is no space in "\t\n", you mean " \t\n".

2) The call to trim trailing spaces is simply incorrect, it does nothing
of the sort. It's more likely to crash your program than anything else.

john
Oct 13 '05 #4

P: n/a
Thanks John!
I did as you suggested, but it causes a segmentation fault.
I get a message saying: 'This application has requested the Runntime to
terminate it in an unusual way.'
I am using GCC-g++-3.4.2 on a MSWin-XP.

I would like to know if you have a tangible example showing how to
solve this problem; if so, could you share it?

TIA

Oct 13 '05 #5

P: n/a
std::string trimIt( std::string s ) {
size_t offset = s.length();
// Trim leading spaces ???
s.erase( 0, s.find_first_not_of( " \t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_last_not_of( " \t\n" ) + 1);
// This line is always displayed
if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
return s;
}

Does this work for you?
Note: in the original post you wasn't passing the string by reference
so i left it unchanged.

Oct 13 '05 #6

P: n/a
Yes!!
Thanks Paulius-maruska, it did the trick.

Oct 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.