Blame view

sip/Utils/StringTools.hpp 1.2 KB
c887a0f0   Hu Chunming   提交初成版代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  #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