修剪STD:String的最佳方法是什么?我目前正在使用以下代碼對所有std::strings在我的節(jié)目中:std::string s;s.erase(s.find_last_not_of(" \n\r\t")+1);它很好,但我想知道是否有一些終結的情況下,它可能會失?。慨斎?,答案與優(yōu)雅的替代方案和左修剪解決方案也是受歡迎的。
3 回答

慕村9548890
TA貢獻1884條經(jīng)驗 獲得超4個贊
std::strings
(
// trim trailing spacessize_t endpos = str.find_last_not_of(" \t");size_t startpos = str.find_first_not_of(" \t"); if( std::string::npos != endpos ){ str = str.substr( 0, endpos+1 ); str = str.substr( startpos );}else { str.erase(std::remove(std::begin(str), std::end(str), ' '), std::end(str));}
// trim leading spacessize_t startpos = str.find_first_not_of(" \t");if( string::npos != startpos ){ str = str.substr( startpos );}
- 3 回答
- 0 關注
- 894 瀏覽
添加回答
舉報
0/150
提交
取消