Skip to content

Instantly share code, notes, and snippets.

@cmaughan
Created January 14, 2016 13:49
Show Gist options
  • Save cmaughan/448e735c7b2da6854deb to your computer and use it in GitHub Desktop.
Save cmaughan/448e735c7b2da6854deb to your computer and use it in GitHub Desktop.
Trimming sample
auto Dump = [](std::string name)
{
// trim from end of string (right)
auto RTrim = [](std::string& s, const char* t)
-> std::string&
{
s.erase(s.find_last_not_of(t) + 1);
return s;
};
// trim from beginning of string (left)
auto LTrim = [](std::string& s, const char* t)
-> std::string&
{
s.erase(0, s.find_first_not_of(t));
return s;
};
// trim from both ends of string (left & right)
auto Trim = [&](std::string& s, const char* t)
-> std::string&
{
return LTrim(RTrim(s, t), t);
};
std::string out = Trim(name, "0123456789");
out = RTrim(out, " \n\t");
out = std::string("{ \"") + out + std::string("\", \"tag\" },\n");
OutputDebugStringA(out.c_str());
};
@cmaughan
Copy link
Author

Just a little lambda example of trimming a std::string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment