469,636 Members | 1,516 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Removing the leading and tailing blanks

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
6 5773
> trimIt( std::string s ) {

First, try this...

trimIt( std::string& s) {

Oct 13 '05 #2
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
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
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
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
Yes!!
Thanks Paulius-maruska, it did the trick.

Oct 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by qwweeeit | last post: by
10 posts views Thread by Tony Stock | last post: by
4 posts views Thread by clickon | last post: by
5 posts views Thread by nuffnough | last post: by
1 post views Thread by Shaw | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.