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 小高 閱讀(1937) 評論(0)  編輯  收藏 所屬分類: C

          導航

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

          統計

          常用鏈接

          留言簿(3)

          隨筆分類(352)

          收藏夾(19)

          關注的blog

          手冊

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 巴彦县| 礼泉县| 澳门| 娄底市| 陆河县| 胶南市| 宣城市| 屏山县| 曲沃县| 南涧| 淅川县| 淳化县| 正宁县| 彭水| 绥芬河市| 肇庆市| 扶风县| 措美县| 阿图什市| 正蓝旗| 泉州市| 商都县| 武平县| 洮南市| 白银市| 正蓝旗| 大方县| 怀宁县| 额敏县| 蒙自县| 永春县| 泾川县| 多伦县| 德昌县| 沁源县| 阿坝县| 巩留县| 宁夏| 潜山县| 内乡县| 汉川市|