invalid conversion from ‘const char*’ to ‘char’

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

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

1. Using boost::algorithm

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

string str1(" hello world! ");

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));

登录 *

loading captcha image...
or Ctrl+Enter