修剪STD:String的最佳方法是什么?我目前正在使用以下代码对所有std::strings在我的节目中:std::string s;s.erase(s.find_last_not_of(" \n\r\t")+1);它很好,但我想知道是否有一些终结的情况下,它可能会失败?当然,答案与优雅的替代方案和左修剪解决方案也是受欢迎的。
3 回答
慕村9548890
TA贡献1884条经验 获得超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 关注
- 914 浏览
添加回答
举报
0/150
提交
取消
