Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

c++ - 修剪std :: string的最佳方法是什么?(What's the best way to trim std::string?)

I'm currently using the following code to right-trim all the std::strings in my programs:

(我目前正在使用以下代码对程序中的所有std::strings进行右修剪:)

std::string s;
s.erase(s.find_last_not_of(" 

")+1);

It works fine, but I wonder if there are some end-cases where it might fail?

(它可以正常工作,但我想知道是否存在某些可能会失败的最终情况?)

Of course, answers with elegant alternatives and also left-trim solution are welcome.

(当然,我们欢迎您提供其他优雅的解决方案以及左修剪解决方案。)

  ask by Milan Babu?kov translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 &ltrim(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.

(我出于上下文的考虑而保留了上面的原始答案,并且为了使获得高票的答案仍然可用。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...