Skip to content

Instantly share code, notes, and snippets.

@xylcbd
Created June 21, 2016 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xylcbd/064e7c42d1f37f435679e8d9b4acc72a to your computer and use it in GitHub Desktop.
Save xylcbd/064e7c42d1f37f435679e8d9b4acc72a to your computer and use it in GitHub Desktop.
c++ string about utils code
static std::string strip(const std::string& content)
{
if (content.empty())
{
return content;
}
std::string result = content;
while (true)
{
const wchar_t last_char = result[result.size() - 1];
if (last_char == '\r' ||
last_char == '\n' ||
last_char == ' ' ||
last_char == ',' ||
last_char == '\t')
{
result = result.substr(0, result.size() - 1);
}
else
{
break;
}
}
return result;
}
static std::vector<std::string> splitString(const std::string& s, const std::string& c)
{
std::vector<std::string> v;
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
return v;
}
static std::string getFileContent(const std::string& filePath)
{
std::string result;
std::ifstream ifs(filePath);
if (!ifs.is_open())
{
return result;
}
while (!ifs.eof())
{
std::string line;
std::getline(ifs, line);
result += line + "\n";
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment