StringTools.hpp 1.2 KB
#ifndef __STRING_TOOLS_HPP__
#define __STRING_TOOLS_HPP__

#include <string>
#include <algorithm>
#include <vector>

using namespace std;

namespace StringTools {

    static bool isntspace(const char &ch) {
        return !isspace(ch);
    }

    static std::string trim(const std::string &s) {
        std::string::const_iterator iter1 = find_if(s.begin(), s.end(), isntspace);
        std::string::const_iterator iter2 = find_if(s.rbegin(), s.rend(), isntspace).base();

        return iter1 < iter2 ? string(iter1, iter2) : std::string("");
    }

    static vector<string> split(const string &origin,const string &sep){
        size_t found = origin.find(sep);
        size_t s = 0;
        std::vector<string> vec;
        while (found != string::npos)
        {
            string kv = origin.substr(s,found - s);
            vec.push_back(trim(kv));
            s = found + 1;
            found = origin.find(sep,s);
        }
        vec.push_back(origin.substr(s,origin.length() - s));
        return vec;
    }

    static string to_lower(string origin) {
        string str_lower = trim(origin);
        transform(str_lower.begin(), str_lower.end(), str_lower.begin(),::tolower);
        return str_lower;
    }
}

#endif