C++技巧之std::string常用算法[1]

STL的string已给我提供一些常用的算法(find,substr,erase,replace等),但在实现应用中远远不能满足需求,像trim,toupper,tolower这样简单的算法都不提供,我曾偶发现一个开源的C++库中简洁优雅实现了这些算法。

1.trimLeft

template <class S> S trimLeft(const S& str) /// Returns a copy of str with all leading /// whitespace removed. { typename S::const_iterator it = str.begin(); typename S::const_iterator end = str.end(); while (it != end && std::isspace(*it)) ++it; return S(it, end); }

2.trimLeftInPlace

template <class S> S& trimLeftInPlace(S& str) /// Removes all leading whitespace in str. { typename S::iterator it = str.begin(); typename S::iterator end = str.end(); while (it != end && std::isspace(*it)) ++it; str.erase(str.begin(), it); return str; }  

3.trimRight

template <class S> S trimRight(const S& str) /// Returns a copy of str with all trailing /// whitespace removed. { int pos = int(str.size()) - 1; while (pos >= 0 && std::isspace(str[pos])) --pos; return S(str, 0, pos + 1); }

4.trimRightInPlace

template <class S> S& trimRightInPlace(S& str) /// Removes all trailing whitespace in str. { int pos = int(str.size()) - 1; while (pos >= 0 && std::isspace(str[pos])) --pos; str.resize(pos + 1); return str; }

5.trim

template <class S> S trim(const S& str) /// Returns a copy of str with all leading and /// trailing whitespace removed. { int first = 0; int last = int(str.size()) - 1; while (first <= last && std::isspace(str[first])) ++first; while (last >= first && std::isspace(str[last])) --last; return S(str, first, last - first + 1); }

6.trimInPlace

template <class S> S& trimInPlace(S& str) /// Removes all leading and trailing whitespace in str. { int first = 0; int last = int(str.size()) - 1; while (first <= last && std::isspace(str[first])) ++first; while (last >= first && std::isspace(str[last])) --last; str.resize(last + 1); str.erase(0, first); return str; }

7.toUpper

template <class S> S toUpper(const S& str) /// Returns a copy of str containing all upper-case characters. { typename S::const_iterator it = str.begin(); typename S::const_iterator end = str.end(); S result; result.reserve(str.size()); while (it != end) result += std::toupper(*it++); return result; }

8.toUpperInPlace

template <class S> S& toUpperInPlace(S& str) /// Replaces all characters in str with their upper-case counterparts. { typename S::iterator it = str.begin(); typename S::iterator end = str.end(); while (it != end) { *it = std::toupper(*it); ++it; } return str; }

9.toLower

template <class S> S toLower(const S& str) /// Returns a copy of str containing all lower-case characters. { typename S::const_iterator it = str.begin(); typename S::const_iterator end = str.end(); S result; result.reserve(str.size()); while (it != end) result += std::tolower(*it++); return result; }

10.toLowerInPlace

template <class S> S& toLowerInPlace(S& str) /// Replaces all characters in str with their lower-case counterparts. { typename S::iterator it = str.begin(); typename S::iterator end = str.end(); while (it != end) { *it = std::tolower(*it); ++it; } return str; }

你可能感兴趣的:(C++,算法,String,iterator,Class,whitespace)