Skip to content

Instantly share code, notes, and snippets.

@alenstarx
Created March 21, 2016 09:42
Show Gist options
  • Save alenstarx/878094db90070fb00838 to your computer and use it in GitHub Desktop.
Save alenstarx/878094db90070fb00838 to your computer and use it in GitHub Desktop.
// trim from start
static inline std::string &string_ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &string_rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &string_trim(std::string &s) {
return string_ltrim(string_rtrim(s));
}
std::string string_replace(std::string& str, const std::string from, const std::string to) {
size_t start_pos = str.find(from);
if (start_pos == std::string::npos)
return str;
str.replace(start_pos, from.length(), to);
return str;
}
std::vector<std::string> string_split(const std::string& src, std::string separate_character)
{
std::vector<std::string> strs;
int separate_characterLen = separate_character.size();
int last_position = 0, index = -1;
while (-1 != (index = src.find(separate_character, last_position)))
{
std::string sss = src.substr(last_position, index - last_position);
if (sss.size())
strs.push_back(sss);
last_position = index + separate_characterLen;
}
std::string lastString = src.substr(last_position);
if (!lastString.empty())
if (lastString.size())
strs.push_back(lastString);
return strs;
}
std::string string_replace_all(const std::string &src, std::string org_str, std::string rep_str)
{
std::vector<std::string> delimVec = string_split(src, org_str);
if (delimVec.size() <= 0){
return src;
}
std::string target("");
std::vector<std::string>::iterator it = delimVec.begin();
for (; it != delimVec.end(); ++it)
{
target += (*it) + rep_str;
}
return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment