4 ms·
std::wstring remove_side_spaces(const std::wstring& str) { auto begin = std::find_if_not(str.begin(), str.end(), std::iswspace); if (begin == st
by pdhborges 14y ago
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());
}
}
- jlarocco 14y agoBelieve it or not, that doesn't compile because begin and end have different types due to begin/end returning different iterator types than rbegin/rend. It could be a bug in both clang and g++. 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; }
- pdhborges 14y agoI also corrected my example. Thanks!