Created
January 14, 2016 13:49
-
-
Save cmaughan/448e735c7b2da6854deb to your computer and use it in GitHub Desktop.
Trimming sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a little lambda example of trimming a std::string.