The code in the blog post removes only leading or trailing white space, not all of it.
std::wstring remove_side_spaces(const std::wstring& str) {
auto begin = std::find_if_not(str.begin(), str.end(), std::iswspace);
if (begin == str.end()) {
return std::wstring();
} else {
auto end = std::find_if_not(str.rbegin(), str.rend(), std::iswspace);
return std::wstring(begin, end.base());
}
}This works, but I don't like the &strt and &endp:
std::wstring trim(std::wstring theStr) {
auto strt = std::find_if_not(theStr.begin(), theStr.end(), std::iswspace);
if (strt == theStr.end()) return L"";
auto endp = std::find_if_not(theStr.rbegin(), theStr.rend(), std::iswspace);
return std::wstring(&*strt,&*endp+1);
}
The easiest way is to use Boost's string algorithms library, though: std::wstring trimb(std::wstring theStr) {
boost::trim(theStr);
return theStr;
}