Removing the leading and tailing blanks

J

jalkadir

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
 
B

ben

jalkadir said:
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
 
J

John Harrison

jalkadir said:
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
 
J

jalkadir

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
 
P

paulius-maruska

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top