JUST DO IT ~

          我只想當個程序員

          c++ c stl boost trim 函數

          c++ c stl boost  trim 函數


          參考:

          http://www.cplusplus.com/faq/sequences/strings/trim/

          What's the best way to trim std::string
          http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring

           

          其中這個方案我喜歡:

          #include <algorithm>
          #include <functional>
          #include <cctype>
          #include <locale>

          // trim from start
          static inline std::string &ltrim(std::string &s) {
                  s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
                  return s;
          }

          // trim from end
          static inline std::string &rtrim(std::string &s) {
                  s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
                  return s;
          }

          // trim from both ends
          static inline std::string &trim(std::string &s) {
                  return ltrim(rtrim(s));
          }

           
          BOOST 方案 : 
          #include <boost/algorithm/string.hpp>
          using namespace std;
          using namespace boost::algorithm;
          
          string str1(" hello world! ");
          trim(str1);
          
          // str1 is now "hello world!"
          // Use trim_right() if only trailing whitespace is to be removed.
          最剪短方案:
          std::string s;
          s.erase(s.find_last_not_of(" \n\r\t")+1);
           

          posted on 2015-03-20 09:39 小高 閱讀(1927) 評論(0)  編輯  收藏 所屬分類: C

          導航

          <2015年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          統計

          常用鏈接

          留言簿(3)

          隨筆分類(352)

          收藏夾(19)

          關注的blog

          手冊

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 张家口市| 沂源县| 怀来县| 绩溪县| 六枝特区| 远安县| 新营市| 阜阳市| 晴隆县| 柏乡县| 大丰市| 青神县| 石河子市| 登封市| 聊城市| 龙门县| 海南省| 潮安县| 横峰县| 永新县| 铁力市| 同德县| 平武县| 广德县| 巴东县| 吕梁市| 聂荣县| 衡山县| 阿拉善盟| 塔城市| 商丘市| 清徐县| 安阳县| 时尚| 濮阳市| 临沧市| 石家庄市| 宜阳县| 利川市| 浦北县| 惠水县|