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

          導航

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

          統計

          常用鏈接

          留言簿(3)

          隨筆分類(352)

          收藏夾(19)

          關注的blog

          手冊

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 泰来县| 托克逊县| 垣曲县| 新乐市| 德州市| 定远县| 肇东市| 剑川县| 永春县| 潞西市| 福州市| 桦南县| 蓬安县| 曲靖市| 邯郸市| 仲巴县| 正安县| 尚志市| 诸暨市| 泸定县| 鄯善县| 岳阳县| 荔波县| 江陵县| 临洮县| 万盛区| 崇义县| 扶余县| 丽水市| 静安区| 石泉县| 永川市| 文水县| 广安市| 安乡县| 聂拉木县| 衡阳县| 陇西县| 广昌县| 岑溪市| 晋中市|