Advertisement
STANAANDREY

remove_beg&end_spaces

Jul 3rd, 2019
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5. const std::string WHITESPACE = " \n\r\t\f\v";
  6.  
  7. std::string ltrim(const std::string& s)
  8. {
  9.     size_t start = s.find_first_not_of(WHITESPACE);
  10.     return (start == std::string::npos) ? "" : s.substr(start);
  11. }
  12.  
  13. std::string rtrim(const std::string& s)
  14. {
  15.     size_t e = s.find_last_not_of(WHITESPACE);
  16.     return (e == std::string::npos) ? "" : s.substr(0, e + 1);
  17. }
  18.  
  19. std::string trim(const std::string& s)
  20. {
  21.     return rtrim(ltrim(s));
  22. }
  23.  
  24. int main()
  25. {
  26.     std::string s[10];
  27.     s[1] = "\n\t                    g hwetHello World \r\n ";
  28.     std::cout << "START::" << trim(s[1]) << "::end\n";
  29.     s[2]="  jkhgdlkj ";
  30.     cout<<trim(s[2]);
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement