源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

c++ String去除头尾空格的方法

  • 时间:2021-02-10 20:38 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:c++ String去除头尾空格的方法
本文实例讲述了c++ String去除头尾空格的方法,分享给大家供大家参考。具体实现方法如下: 实现该功能可使用string的find_first_not_of,和find_last_not_of方法,具体实现带如下:
[u]复制代码[/u] 代码如下:
#include <iostream> #include <string> std::string& trim(std::string &); int main() {     std::string s = " Hello World!! ";     std::cout << s << " size:" << s.size() << std::endl;     std::cout << trim(s) << " size:" << trim(s).size() << std::endl;     return 0; } std::string& trim(std::string &s) {     if (s.empty())     {         return s;     }     s.erase(0,s.find_first_not_of(" "));     s.erase(s.find_last_not_of(" ") + 1);     return s; }
希望本文所述对大家的C++程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部