C++ 中的类似功能到Python的strip()?

发布于 2025-01-07 01:23:38 字数 150 浏览 1 评论 0原文

Python中有一个非常有用的函数,叫做 strip()。 C++中有类似的吗?

There is a very useful function in Python called strip(). Any similar ones in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哀由 2025-01-14 01:23:38

我用这个:

#include <string>
#include <cctype>

std::string strip(const std::string &inpt)
{
    auto start_it = inpt.begin();
    auto end_it = inpt.rbegin();
    while (std::isspace(*start_it))
        ++start_it;
    while (std::isspace(*end_it))
        ++end_it;
    return std::string(start_it, end_it.base());
}

I use this:

#include <string>
#include <cctype>

std::string strip(const std::string &inpt)
{
    auto start_it = inpt.begin();
    auto end_it = inpt.rbegin();
    while (std::isspace(*start_it))
        ++start_it;
    while (std::isspace(*end_it))
        ++end_it;
    return std::string(start_it, end_it.base());
}
凉墨 2025-01-14 01:23:38

没有任何内置的东西;我曾经使用类似以下的东西:

template <std::ctype_base::mask mask>
class IsNot
{
    std::locale myLocale;       // To ensure lifetime of facet...
    std::ctype<char> const* myCType;
public:
    IsNot( std::locale const& l = std::locale() )
        : myLocale( l )
        , myCType( &std::use_facet<std::ctype<char> >( l ) )
    {
    }
    bool operator()( char ch ) const
    {
        return ! myCType->is( mask, ch );
    }
};

typedef IsNot<std::ctype_base::space> IsNotSpace;

std::string
trim( std::string const& original )
{
    std::string::const_iterator right = std::find_if( original.rbegin(), original.rend(), IsNotSpace() ).base();
    std::string::const_iterator left = std::find_if(original.begin(), right, IsNotSpace() );
    return std::string( left, right );
}

效果很好。 (我现在有一个更加复杂的
正确处理 UTF-8 的版本。)

There's nothing built-in; I used to use something like the following:

template <std::ctype_base::mask mask>
class IsNot
{
    std::locale myLocale;       // To ensure lifetime of facet...
    std::ctype<char> const* myCType;
public:
    IsNot( std::locale const& l = std::locale() )
        : myLocale( l )
        , myCType( &std::use_facet<std::ctype<char> >( l ) )
    {
    }
    bool operator()( char ch ) const
    {
        return ! myCType->is( mask, ch );
    }
};

typedef IsNot<std::ctype_base::space> IsNotSpace;

std::string
trim( std::string const& original )
{
    std::string::const_iterator right = std::find_if( original.rbegin(), original.rend(), IsNotSpace() ).base();
    std::string::const_iterator left = std::find_if(original.begin(), right, IsNotSpace() );
    return std::string( left, right );
}

which works pretty well. (I now have a significantly more complex
version which handles UTF-8 correctly.)

爺獨霸怡葒院 2025-01-14 01:23:38
void strip(std::string &str)
{
    if  (str.length() != 0)
    {
    auto w = std::string(" ") ;
    auto n = std::string("\n") ;
    auto r = std::string("\t") ;
    auto t = std::string("\r") ;
    auto v = std::string(1 ,str.front()); 
    while((v == w) || (v==t) || (v==r) || (v==n))
    {
        str.erase(str.begin());
        v = std::string(1 ,str.front());
    }
    v = std::string(1 , str.back()); 
    while((v ==w) || (v==t) || (v==r) || (v==n))
    {
        str.erase(str.end() - 1 );
        v = std::string(1 , str.back());
    }
}
void strip(std::string &str)
{
    if  (str.length() != 0)
    {
    auto w = std::string(" ") ;
    auto n = std::string("\n") ;
    auto r = std::string("\t") ;
    auto t = std::string("\r") ;
    auto v = std::string(1 ,str.front()); 
    while((v == w) || (v==t) || (v==r) || (v==n))
    {
        str.erase(str.begin());
        v = std::string(1 ,str.front());
    }
    v = std::string(1 , str.back()); 
    while((v ==w) || (v==t) || (v==r) || (v==n))
    {
        str.erase(str.end() - 1 );
        v = std::string(1 , str.back());
    }
}
那些过往 2025-01-14 01:23:38

这是 Ferdi Kedef 提供的答案之上的,以使其更安全。

void strip(std::string& str)
{
    if (str.length() == 0) {
        return;
    }

    auto start_it = str.begin();
    auto end_it = str.rbegin();
    while (std::isspace(*start_it)) {
        ++start_it;
        if (start_it == str.end()) break;
    }
    while (std::isspace(*end_it)) {
        ++end_it;
        if (end_it == str.rend()) break;
    }
    int start_pos = start_it - str.begin();
    int end_pos = end_it.base() - str.begin();
    str = start_pos <= end_pos ? std::string(start_it, end_it.base()) : "";
}

This is on top of the answer provided by Ferdi Kedef to make it safer.

void strip(std::string& str)
{
    if (str.length() == 0) {
        return;
    }

    auto start_it = str.begin();
    auto end_it = str.rbegin();
    while (std::isspace(*start_it)) {
        ++start_it;
        if (start_it == str.end()) break;
    }
    while (std::isspace(*end_it)) {
        ++end_it;
        if (end_it == str.rend()) break;
    }
    int start_pos = start_it - str.begin();
    int end_pos = end_it.base() - str.begin();
    str = start_pos <= end_pos ? std::string(start_it, end_it.base()) : "";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文