C++/공백제거 trim

  1. template<typename T>
  2. inline T TrimStr_Generic(const T& Src, const T& c)
  3. {
  4.     int p2 = Src.find_last_not_of(c);
  5.     if (p2 == T::npos) return T();
  6.     int p1 = Src.find_first_not_of(c);
  7.     if (p1 == T::npos) p1 = 0;
  8.     return Src.substr(p1, (p2-p1)+1);
  9. }
  10. char* trim(char *source)
  11. {
  12.     std::string str(source);
  13.     str = TrimStr_Generic<std::string>(str, "\r");
  14.     str = TrimStr_Generic<std::string>(str, "\n");
  15.     str = TrimStr_Generic<std::string>(str, "\t");
  16.     str = TrimStr_Generic<std::string>(str, " "); 
  17.     strcpy_s(source, str.length() + 1, str.c_str());
  18.     return source;
  19. }

"  testing 134     \r\n" ==> "testing 134"

이 글에는 0 개의 댓글이 있습니다.