使用stl::string去除空白字符
2009-12-16 17:02
375 查看
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 }
(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 }
相关文章推荐
- C# 使用String.Join 和 Distinct 方法 去除字符串中重复字符
- 如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++)
- string 中去除空白字符
- (转帖)如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++)
- (原創) 如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++)
- 使用stringByTrimmingCharactersInSet ,NSCharacterSet 去除空格
- IOS中字符串去除特殊字符StringByTrimmingCharatersInSet
- 关于代码中空白、注释等字符的统计(正则表达式的使用)
- STL:string常用函数方法使用介绍
- php去除换行符和空格等字符的方法小结(PHP_EOL变量的使用)
- python去除空白字符
- 标准模板库(STL)的 std::string 与Unicode的使用
- PHP使用正则表达式过滤特殊的字符和空白
- stl string 容器的使用
- string基本字符序列容器(竞赛时常用的使用方法总结)
- 【C++】统计string里面出现的字符的个数(使用count函数)
- 使用ltrim或rtrim去除字符
- stl下string的用法之取路劲和截断字符
- 关于一道J笔试或者机试题的Java实现:从键盘输入一串字符,翻转后输出(要求不使用string相关类即对象)
- StringUtil工具类之去除所有的空白字符