MUQ  0.4.3
StringUtilities.cpp
Go to the documentation of this file.
2 
3 using namespace std;
4 
5 std::vector<std::string> muq::Utilities::StringUtilities::Split(std::string str, char delim)
6 {
7  std::vector<std::string> output;
8 
9  int pos = 0;
10  std::string strPart;
11  // while we keep finding commas...
12  while ((pos = str.find(delim)) != std::string::npos) {
13 
14  // extract the part before the comma
15  strPart = str.substr(0, pos);
16 
17  // erase the part before the comma
18  str.erase(0, pos + 1);
19 
20  // Strip whitespace and store the section name
21  output.push_back(muq::Utilities::StringUtilities::Strip(strPart));
22  }
23 
24  // make sure to store whatevers left
25  output.push_back(muq::Utilities::StringUtilities::Strip(str));
26 
27  return output;
28 }
29 
30 
31 std::string muq::Utilities::StringUtilities::Combine(std::vector<std::string> strs, char delim)
32 {
33  std::string output = strs.at(0);
34  for(unsigned int i=1; i<strs.size(); ++i){
35  output.push_back(delim);
36  output += strs.at(i);
37  }
38  return output;
39 }
40 
41 
42 std::string muq::Utilities::StringUtilities::Strip(std::string str)
43 {
44  // remove whitespace from the beginning
45  while((str.front()==' ')||(str.front()=='\t')||(str.front()=='\n'))
46  str.erase(0,1);
47 
48  // remove whitespace from the end
49  while((str.back()==' ')||(str.back()=='\t')||(str.back()=='\n'))
50  str.erase(str.size()-1,1);
51 
52  return str;
53 }
std::string Combine(std::vector< std::string > strs, char delim=',')
Combine a vector of strings with a specified delimiter. The opposite of Split.
std::string Strip(std::string str)
Strip the whitespace off the beginning and end of a string.
std::vector< std::string > Split(std::string str, char delim=',')
Split a string into parts based on a particular character delimiter. Also Strips whitespace from part...