20 std::string&
trim(std::string* str) {
21 return trim(str,
" \r\n\t");
24 std::string&
trim(std::string* str,
const char* drop) {
25 std::string::size_type pos = str->find_last_not_of(drop);
26 if (pos != std::string::npos) {
28 pos = str->find_first_not_of(drop);
29 if (pos != std::string::npos) str->erase(0, pos);
32 str->erase(str->begin(), str->end());
37 std::string&
trim(std::string* str,
const std::string& drop) {
38 std::string::size_type pos = str->find_last_not_of(drop);
39 if (pos != std::string::npos) {
41 pos = str->find_first_not_of(drop);
42 if (pos != std::string::npos) str->erase(0, pos);
45 str->erase(str->begin(), str->end());
50 std::string
trim(
const std::string& str) {
51 return trim(str,
" \r\n\t");
54 std::string
trim(
const std::string& str,
const char* drop) {
56 std::string::size_type pos1 = str.find_first_not_of(drop);
57 if (pos1 == std::string::npos)
return std::string();
60 std::string out = str.substr(pos1, std::string::npos);
63 std::string::size_type pos2 = out.find_last_not_of(drop);
64 if (pos2 != std::string::npos)
70 std::string
trim(
const std::string& str,
71 const std::string& drop) {
73 out.reserve(str.size());
76 std::string::const_iterator it = str.begin();
77 while (it != str.end() && drop.find(*it) != std::string::npos)
81 out.resize(str.end() - it);
82 std::copy(it, str.end(), out.begin());
85 std::string::size_type pos = out.find_last_not_of(drop);
86 if (pos != std::string::npos)
98 std::string&
trim_right(std::string* str,
const char* drop) {
99 str->erase(str->find_last_not_of(drop) + 1, std::string::npos);
103 std::string&
trim_right(std::string* str,
const std::string& drop) {
104 str->erase(str->find_last_not_of(drop) + 1, std::string::npos);
112 std::string
trim_right(
const std::string& str,
const char* drop) {
113 std::string::size_type pos = str.find_last_not_of(drop);
114 if (pos == std::string::npos)
return std::string();
116 return str.substr(0, pos + 1);
119 std::string
trim_right(
const std::string& str,
const std::string& drop) {
120 std::string::size_type pos = str.find_last_not_of(drop);
121 if (pos == std::string::npos)
return std::string();
123 return str.substr(0, pos + 1);
132 std::string&
trim_left(std::string* str,
const char* drop) {
133 str->erase(0, str->find_first_not_of(drop));
137 std::string&
trim_left(std::string* str,
const std::string& drop) {
138 str->erase(0, str->find_first_not_of(drop));
146 std::string
trim_left(
const std::string& str,
const char* drop) {
147 std::string::size_type pos = str.find_first_not_of(drop);
148 if (pos == std::string::npos)
return std::string();
150 return str.substr(pos, std::string::npos);
153 std::string
trim_left(
const std::string& str,
const std::string& drop) {
154 std::string::size_type pos = str.find_first_not_of(drop);
155 if (pos == std::string::npos)
return std::string();
157 return str.substr(pos, std::string::npos);
std::string & trim(std::string *str)
Trims the given string in-place on the left and right.
std::string & trim_right(std::string *str)
Trims the given string in-place only on the right.
std::string & trim_left(std::string *str)
Trims the given string in-place only on the left.