Advertisement
pneave

std::string trim functions

Jan 15th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. // trim from start (in place)
  2. static inline void ltrim(std::string &s) {
  3.    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
  4.       return !std::isspace(ch);
  5.    }));
  6. }
  7.  
  8. // trim from end (in place)
  9. static inline void rtrim(std::string &s) {
  10.    s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
  11.       return !std::isspace(ch);
  12.    }).base(), s.end());
  13. }
  14.  
  15. // trim from both ends (in place)
  16. static inline void trim(std::string &s) {
  17.    ltrim(s);
  18.    rtrim(s);
  19. }
  20.  
  21. // trim from start (copying)
  22. static inline std::string ltrim_copy(std::string s) {
  23.    ltrim(s);
  24.    return s;
  25. }
  26.  
  27. // trim from end (copying)
  28. static inline std::string rtrim_copy(std::string s) {
  29.    rtrim(s);
  30.    return s;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement