void IO::TrimRight(std::basic_string<charType> & str, const char* chars2remove) { if (!str.empty()) { //trim the characters in chars2remove from the right std::string::size_type pos = 0; if (chars2remove != NULL) { pos = str.find_last_not_of(chars2remove); if (pos != std::string::npos) str.erase(pos+1); else str.erase( str.begin() , str.end() ); // make empty } else { //trim space pos = std::string::npos; for (int i = str.size()-1; i >= 0; --i) { if (!isspace(str[i])) { pos = i; break; } } if (pos != std::string::npos) { if (pos+1 != str.size()) str.resize(pos+1); } else { str.clear(); } } } }
static inline std::basic_string<_CharT, _Traits, _Alloc> rstrip( const std::basic_string<_CharT, _Traits, _Alloc> &a, const strip_t stripchars) { typedef std::basic_string<_CharT, _Traits, _Alloc> str_t; typename str_t::size_type p = a.find_last_not_of(stripchars); if (p == str_t::npos) return ""; return a.substr(0, p + 1); }
template<typename char_t> std::basic_string<char_t> trim(const std::basic_string<char_t>& what) { if ( what.empty() ) return what; const char_t whitespace[3] = { char_t(' '), char_t('\t'), 0 }; size_t left = what.find_first_not_of(whitespace); size_t right = what.find_last_not_of(whitespace); if ( left == std::basic_string<char_t>::npos ) { return std::basic_string<char_t>(); } else { return what.substr(left, right-left+1); } }