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( "  \t " )  +   1 );
5       return  result.erase( 0 , result.find_first_not_of( "  \t " ));
6  }

1  std:: string &  trim(std:: string   & s) {
 2  23    if  (s.empty()) {
 3  24      return  s;
 4  25   }
 5  26
 6  27   s.erase( 0 ,s.find_first_not_of( "   " ));
 7  28   s.erase(s.find_last_not_of( "   " )  +   1 );
 8  29    return  s;
 9  30 }
10

转载于:https://www.cnblogs.com/allen_yu/archive/2009/12/16/1625694.html

使用stl::string去除字符串前后的空白相关推荐

  1. go去掉最后一个字符_Go语言去除字符串中的指定字符

    Go语言中常用的去除字符串中指定字符的方法如下表所示. 表:字符串处理函数 方法功能描述 func Trim(s string, cutset string) string 将字符串 s 首尾包含在 ...

  2. python去掉两边空格_Python模拟删除字符串两边的空白

    目标: 1.使用string模块的whitespace 2.删除左边.右边以及两边的空白 代码如下: [root@localhost python]# cat rmspace.py #!/usr/bi ...

  3. string去除空格

    自己写的去除空格方法:   Trim(string):去除字符串两边的空格             LTrim(string):去除左边的空格             function   LTrim ...

  4. Python去除字符串前后空格的几种方法

    对程序员来说,'python' 和'python ' 看起来几乎没什么两样,但对程序来说,它们却是两个不同的字符串.Python能够发现'python ' 中额外的空白,并认为它是有意义的. 空白很重 ...

  5. 如何将字符串前后的空白去除(C/C++) (STL)

    Abstract 在(原創) 如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++) (STL) 中已 ...

  6. (原創) 如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++)...

    这在字符串处理是很常用的功能,.NET Framework的String class直接提供Trim()的method,其它语言也大都有提供(VB.VFP),但C++无论Standard Librar ...

  7. C++ 笔记(22)— STL string 类(字符串赋值、访问、拼接、查找、翻转、大小写转换)

    1. 实例化和赋值 STL string #include <string> #include <iostream>int main () {using namespace s ...

  8. stl string 函数_使用C ++ STL中的string :: append()函数将文本追加到字符串

    stl string 函数 append() is a library function of <string> header, it is used to append the extr ...

  9. Python中字符串String去除出换行符(\n,\r)和空格的问题

    Python中字符串String去除出换行符和空格的问题(\n,\r) 在Python的编写过程中,获取到的字符串进场存在不明原因的换行和空格,如何整合成一个单句,成为问题. 方法: 一.去除空格 & ...

最新文章

  1. ROS中catkin_make编译输出的可执行文件在哪?
  2. 全球唯一:MySQL社区2018年度公司贡献奖颁给阿里云
  3. 关于 vim 的 magic 设置
  4. 3星|《大产品,小团队》:携程软件开发流程改进的故事
  5. Android定时器AlarmManager(定时发送广播)
  6. 分布式存储系统学习笔记(一)—什么是分布式系统(7)—跨机房部署的三种方案
  7. R语言:企业风险分析(2)【蒙特卡罗模拟,Monte-Carlo Simulation】
  8. 中国电信物联网开放平台对接历程
  9. 天数怎么换算成月_表格当中把天数换算成月怎么
  10. Dell笔记本外接显示器—显示器模糊、字体不清晰
  11. linux添加多网站,linux添加多个网站-微笑の风
  12. node-red实现MQTT通讯
  13. 定量库存控制和定期库存控制
  14. ictclas java 下载,10分钟开始使用ICTCLAS Java版
  15. 从父组件中获取子组件的值
  16. 数据结构——有序链表的合并,链表实现一元多项式相加
  17. Java操作XML的工具:JAXB
  18. 画论80 钱杜《松壶画忆》
  19. 《优化技术实验报告》(附全部可运行代码)校车排版问题建模与分析-CPLEX与MATLAB混合编程
  20. 从CMM的QA到CMMI的PPQA

热门文章

  1. linux中安装redis 使用make install 命令报错
  2. Android音乐播放器的设计与实现
  3. C++ Socket网络编程
  4. iOS日常开发之常用单词、名词注释
  5. 解决IntelliJ IDEA duplicate class R.java and BuildConfig.java
  6. Think Python读书笔记及课后习题---【前三章】
  7. 省市区镇(可以选四级)联动点击自动展开下一级
  8. 【笔试面试考点】PreparedStatement和Statement的区别与联系批量插入数据的优化
  9. docker-compose 安装 Kafka 3.X 附带可视化界面
  10. ZooKeeper 命令操作