trim.h
Go to the documentation of this file.
1 #ifndef cetlib_trim_h
2 #define cetlib_trim_h
3 
4 // ======================================================================
5 //
6 // trim: Remove a string's leading/trailing whitespace
7 //
8 // ======================================================================
9 
10 #include <string>
11 
12 namespace cet {
13 
14  // trim in place:
15  inline std::string& trim_right(std::string& source,
16  std::string const& t = " ");
17  inline std::string& trim_left(std::string& source,
18  std::string const& t = " ");
19  inline std::string& trim(std::string& source, std::string const& t = " ");
20 
21  // trim and return a copy:
23  std::string const& t = " ");
25  std::string const& t = " ");
26  inline std::string trim_copy(std::string source, std::string const& t = " ");
27 }
28 
29 // ----------------------------------------------------------------------
30 // trim in place
31 
34 {
35  return source.erase(1 + source.find_last_not_of(t));
36 }
37 
40 {
41  return source.erase(0, source.find_first_not_of(t));
42 }
43 
46 {
47  return trim_right(source, t), trim_left(source, t);
48 }
49 
50 // ----------------------------------------------------------------------
51 // trim and return a copy
52 
55 {
56  return trim_right(source, t);
57 }
58 
61 {
62  return trim_left(source, t);
63 }
64 
67 {
68  return trim(source, t);
69 }
70 
71 // ======================================================================
72 
73 #endif /* cetlib_trim_h */
74 
75 // Local variables:
76 // mode: c++
77 // End:
std::string & trim(std::string &source, std::string const &t=" ")
Definition: trim.h:45
std::string string
Definition: nybbler.cc:12
std::string & trim_right(std::string &source, std::string const &t=" ")
Definition: trim.h:33
std::string trim_copy(std::string source, std::string const &t=" ")
Definition: trim.h:66
std::string trim_right_copy(std::string source, std::string const &t=" ")
Definition: trim.h:54
std::string & trim_left(std::string &source, std::string const &t=" ")
Definition: trim.h:39
std::string trim_left_copy(std::string source, std::string const &t=" ")
Definition: trim.h:60