charon-core  0.3.1
StringTool.cpp
Go to the documentation of this file.
1 /* This file is part of Charon.
2 
3  Charon is free software: you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7 
8  Charon is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public License
14  along with Charon. If not, see <http://www.gnu.org/licenses/>.
15 */
23 #include "../include/charon-core/StringTool.h"
24 #include <sstream>
25 #include <algorithm>
26 
27 std::string StringTool::trimRight(const std::string& s, const std::string& t) {
28  std::string d(s);
29  std::string::size_type i(d.find_last_not_of(t));
30  if (i == std::string::npos)
31  return "";
32  else
33  return d.erase(d.find_last_not_of(t) + 1);
34 }
35 
36 std::string StringTool::trimLeft(const std::string& s, const std::string& t) {
37  std::string d(s);
38  return d.erase(0, s.find_first_not_of(t));
39 }
40 
41 std::string StringTool::trim(const std::string& s, const std::string& t) {
42  std::string d(s);
43  return trimLeft(trimRight(d, t), t);
44 }
45 
46 void StringTool::explode(std::string str, char delimiter,
47  std::vector<std::string>& result) {
48  std::istringstream strm(str);
49  std::string p;
50  while (std::getline(strm, p, delimiter)) {
51  result.push_back(p);
52  }
53 }
54 
55 std::string StringTool::toLowerCase(std::string s) {
56  transform(s.begin(), s.end(), s.begin(),
57  (int(*)(int)) tolower);
58  return s;
59 }
60 
61 std::string StringTool::combine(std::vector<std::string> strings, char delim)
62 {
63  std::string res="";
64  for(size_t i=0;i<strings.size();i++)
65  {
66  res+=strings[i];
67  if(i<strings.size()-1)
68  res+=delim;
69  }
70  return res;
71 }
std::string charon_core_DLL_PUBLIC trim(const std::string &s, const std::string &t=" \t\r\n")
Remove (whitespace) characters from head and tail of a string.
Definition: StringTool.cpp:41
std::string charon_core_DLL_PUBLIC trimLeft(const std::string &s, const std::string &t=" \t\r\n")
Remove (whitespace) characters from the head of a string.
Definition: StringTool.cpp:36
void charon_core_DLL_PUBLIC explode(std::string str, char delimiter, std::vector< std::string > &result)
Use delimiter to split the string in a list of substrings.
Definition: StringTool.cpp:46
std::string charon_core_DLL_PUBLIC combine(std::vector< std::string > strings, char delim=';')
Combine a vector of strings to one string.
Definition: StringTool.cpp:61
std::string charon_core_DLL_PUBLIC toLowerCase(std::string s)
Convert a string to lowercase.
Definition: StringTool.cpp:55
std::string charon_core_DLL_PUBLIC trimRight(const std::string &s, const std::string &t=" \t\r\n")
Remove (whitespace) characters from the tail of a string.
Definition: StringTool.cpp:27