EDIT Since c++17, some parts of the standard library were removed.
(编辑自c ++ 17起,标准库的某些部分已删除。)
Fortunately, starting with c++11, we have lambdas which are a superior solution. (幸运的是,从c ++ 11开始,我们有了lambda,它们是一种出色的解决方案。)
#include <algorithm>
#include <cctype>
#include <locale>
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
return !std::isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
ltrim(s);
return s;
}
// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
rtrim(s);
return s;
}
// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
trim(s);
return s;
}
Thanks to https://stackoverflow.com/a/44973498/524503 for bringing up the modern solution.
(感谢https://stackoverflow.com/a/44973498/524503提供了现代解决方案。)
Original answer: (原始答案:)
I tend to use one of these 3 for my trimming needs:
(我倾向于使用以下三种之一来满足修整需求:)
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
// trim from start
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
They are fairly self explanatory and work very well.
(它们是自我解释的,并且工作得很好。)
EDIT : BTW, I have std::ptr_fun
in there to help disambiguate std::isspace
because there is actually a second definition which supports locales.
(编辑 :顺便说一句,我在那里有std::ptr_fun
来帮助消除std::isspace
歧义,因为实际上还有第二个支持语言环境的定义。)
This could have been a cast just the same, but I tend to like this better. (这本来可以是相同的演员,但我倾向于更好。)
EDIT : To address some comments about accepting a parameter by reference, modifying and returning it.
(编辑 :解决一些有关通过引用接受参数,修改并返回参数的评论。)
I Agree. (我同意。)
An implementation that I would likely prefer would be two sets of functions, one for in place and one which makes a copy. (我可能更喜欢的一种实现是两组函数,一组用于原位,而另一组进行复制。)
A better set of examples would be: (更好的例子是:)
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
ltrim(s);
return s;
}
// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
rtrim(s);
return s;
}
// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
trim(s);
return s;
}
I am keeping the original answer above though for context and in the interest of keeping the high voted answer still available.
(我出于上下文的考虑而保留了上面的原始答案,并且为了使获得高票的答案仍然可用。)