C++ 修剪 std::string 的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/216823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:48:24  来源:igfitidea点击:

What's the best way to trim std::string?

c++trimstdstring

提问by Milan Babu?kov

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

我目前正在使用以下代码来正确修剪std::strings我的程序中的所有内容:

std::string s;
s.erase(s.find_last_not_of(" \n\r\t")+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.

当然,欢迎提供优雅的替代方案和左修剪解决方案的答案。

回答by Evan Teran

EDITSince c++17, some parts of the standard library were removed. Fortunately, starting with c++11, we have lambdas which are a superior solution.

编辑从 c++17 开始,标准库的某些部分被删除。幸运的是,从 c++11 开始,我们有了 lambdas,这是一个很好的解决方案。

#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/524503for 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:

我倾向于使用以下 3 种中的一种来满足我的修剪需求:

#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_funin there to help disambiguate std::isspacebecause there is actually a second definition which supports locales. This could have been a cast just the same, but I tend to like this better.

编辑:顺便说一句,我std::ptr_fun在那里帮助消除歧义,std::isspace因为实际上有第二个定义支持语言环境。这本来可以是一个演员阵容,但我更喜欢这个。

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.

我保留上面的原始答案,但为了上下文和保持高票答案仍然可用。

回答by Leon Timmermans

Using Boost's string algorithmswould be easiest:

使用Boost 的字符串算法将是最简单的:

#include <boost/algorithm/string.hpp>

std::string str("hello world! ");
boost::trim_right(str);

stris now "hello world!". There's also trim_leftand trim, which trims both sides.

str现在是"hello world!"。还有trim_leftand trim,可以修剪两边。



If you add _copysuffix to any of above function names e.g. trim_copy, the function will return a trimmed copy of the string instead of modifying it through a reference.

如果您_copy为上述任何函数名称添加后缀,例如trim_copy,该函数将返回字符串的修剪副本,而不是通过引用修改它。

If you add _ifsuffix to any of above function names e.g. trim_copy_if, you can trim all characters satisfying your custom predicate, as opposed to just whitespaces.

如果您_if为上述任何函数名称添加后缀,例如trim_copy_if,您可以修剪所有满足您的自定义谓词的字符,而不仅仅是空格。

回答by Bill the Lizard

Use the following code to right trim (trailing) spaces and tab characters from std::strings(ideone):

使用以下代码从std::strings( ideone) 中正确修剪(尾随)空格和制表符:

// trim trailing spaces
size_t endpos = str.find_last_not_of(" \t");
size_t startpos = str.find_first_not_of(" \t");
if( std::string::npos != endpos )
{
    str = str.substr( 0, endpos+1 );
    str = str.substr( startpos );
}
else {
    str.erase(std::remove(std::begin(str), std::end(str), ' '), std::end(str));
}

And just to balance things out, I'll include the left trim code too (ideone):

为了平衡一切,我也会包括左修剪代码(ideone):

// trim leading spaces
size_t startpos = str.find_first_not_of(" \t");
if( string::npos != startpos )
{
    str = str.substr( startpos );
}

回答by Galik

What you are doing is fine and robust. I have used the same method for a long time and I have yet to find a faster method:

你正在做的事情很好而且很健壮。我已经使用相同的方法很长时间了,但我还没有找到更快的方法:

const char* ws = " \t\n\r\f\v";

// trim from end of string (right)
inline std::string& rtrim(std::string& s, const char* t = ws)
{
    s.erase(s.find_last_not_of(t) + 1);
    return s;
}

// trim from beginning of string (left)
inline std::string& ltrim(std::string& s, const char* t = ws)
{
    s.erase(0, s.find_first_not_of(t));
    return s;
}

// trim from both ends of string (right then left)
inline std::string& trim(std::string& s, const char* t = ws)
{
    return ltrim(rtrim(s, t), t);
}

By supplying the characters to be trimmed you have the flexibility to trim non-whitespace characters and the efficiency to trim only the characters you want trimmed.

通过提供要修剪的字符,您可以灵活地修剪非空白字符,并且可以高效地仅修剪您想要修剪的字符。

回答by David G

Bit late to the party, but never mind. Now C++11 is here, we have lambdas and auto variables. So my version, which also handles all-whitespace and empty strings, is:

聚会有点晚,但没关系。现在 C++11 来了,我们有了 lambdas 和 auto 变量。所以我的版本,它也处理全空白和空字符串,是:

#include <cctype>
#include <string>
#include <algorithm>

inline std::string trim(const std::string &s)
{
   auto wsfront=std::find_if_not(s.begin(),s.end(),[](int c){return std::isspace(c);});
   auto wsback=std::find_if_not(s.rbegin(),s.rend(),[](int c){return std::isspace(c);}).base();
   return (wsback<=wsfront ? std::string() : std::string(wsfront,wsback));
}

We could make a reverse iterator from wsfrontand use that as the termination condition in the second find_if_notbut that's only useful in the case of an all-whitespace string, and gcc 4.8 at least isn't smart enough to infer the type of the reverse iterator (std::string::const_reverse_iterator) with auto. I don't know how expensive constructing a reverse iterator is, so YMMV here. With this alteration, the code looks like this:

我们可以从中创建一个反向迭代器wsfront并将其用作第二个中的终止条件,find_if_not但这仅在全空白字符串的情况下才有用,并且 gcc 4.8 至少不够智能以推断反向迭代器的类型(std::string::const_reverse_iterator) 与auto。我不知道构建反向迭代器的成本有多高,所以这里是 YMMV。进行此更改后,代码如下所示:

inline std::string trim(const std::string &s)
{
   auto  wsfront=std::find_if_not(s.begin(),s.end(),[](int c){return std::isspace(c);});
   return std::string(wsfront,std::find_if_not(s.rbegin(),std::string::const_reverse_iterator(wsfront),[](int c){return std::isspace(c);}).base());
}

回答by user818330

Try this, it works for me.

试试这个,它对我有用。

inline std::string trim(std::string& str)
{
    str.erase(0, str.find_first_not_of(' '));       //prefixing spaces
    str.erase(str.find_last_not_of(' ')+1);         //surfixing spaces
    return str;
}

回答by Micha?l Schoonbrood

I like tzaman's solution, the only problem with it is that it doesn't trim a string containing only spaces.

我喜欢 tzaman 的解决方案,唯一的问题是它不会修剪仅包含空格的字符串。

To correct that 1 flaw, add a str.clear() in between the 2 trimmer lines

要纠正那 1 个缺陷,请在 2 条修剪线之间添加 str.clear()

std::stringstream trimmer;
trimmer << str;
str.clear();
trimmer >> str;

回答by Pushkoff

http://ideone.com/nFVtEo

http://ideone.com/nFVtEo

std::string trim(const std::string &s)
{
    std::string::const_iterator it = s.begin();
    while (it != s.end() && isspace(*it))
        it++;

    std::string::const_reverse_iterator rit = s.rbegin();
    while (rit.base() != it && isspace(*rit))
        rit++;

    return std::string(it, rit.base());
}

回答by Greg Hewgill

In the case of an empty string, your code assumes that adding 1 to string::nposgives 0. string::nposis of type string::size_type, which is unsigned. Thus, you are relying on the overflow behaviour of addition.

在空字符串的情况下,您的代码假定将 1 添加到string::npos0.string::npos是类型string::size_type,它是无符号的。因此,您依赖于加法的溢出行为。

回答by Paul Nathan

Hacked off of Cplusplus.com

Cplusplus.com 被

std::string choppa(const std::string &t, const std::string &ws)
{
    std::string str = t;
    size_t found;
    found = str.find_last_not_of(ws);
    if (found != std::string::npos)
        str.erase(found+1);
    else
        str.clear();            // str is all whitespace

    return str;
}

This works for the null case as well. :-)

这也适用于空情况。:-)