invalid conversion from ‘const char*’ to ‘char’
c++文件操作——输入文件包含矩阵形式的数据,输出为vector

Best Ways to Trim a string in C++ [stackoverflow]

Author posted @ 2013年2月19日 05:48 in C/C++ , 2397 阅读

1. Using boost::algorithm

#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost::algorithm;

string str1(" hello world! ");
trim(str1);

2. Define inline function:

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

// trim from start
static inline std::string <rim(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));
}

http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter