您的位置:首页 > 其它

使用stl::string去除空白字符

2009-12-16 17:02 323 查看
erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)

1 // Utility function to trim whitespace off the ends of a string
2 inline std::string trim(std::string source)
3 {
4 std::string result = source.erase(source.find_last_not_of(" ") + 1);
5 return result.erase(0, result.find_first_not_of(" "));
6 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: