string s = "\r\n\t \t test\t\t ";size_t n = s.find_last_not_of( " \r\n\t" );if( n != string::npos ){ s.erase( n + 1 , s.size() - n );}n = s.find_first_not_of ( " \r\n\t" );if( n != string::npos ){ s.erase( 0 , n );}
本文介绍了一种使用C++标准库中的方法来去除字符串首尾空白字符的方法。通过`find_first_not_of`和`find_last_not_of`函数组合使用,可以有效地移除字符串前后的制表符、空格、回车及换行等空白字符。
string s = "\r\n\t \t test\t\t ";size_t n = s.find_last_not_of( " \r\n\t" );if( n != string::npos ){ s.erase( n + 1 , s.size() - n );}n = s.find_first_not_of ( " \r\n\t" );if( n != string::npos ){ s.erase( 0 , n );}
被折叠的 条评论
为什么被折叠?