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
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