【C++】通用函数:过滤string的空行

需要过滤string中的空行,可以网上搜索未果。最终自己写了一个较为通用的函数。

 

输入s表示要过滤的string;

lineend表示行结束符,不同平台的行结束符可能不同。WIn和linux是nl/lf(/n), mac平台则以cr(/r)结尾。

 

std::string DeleteSpaceLineOfStr(const std::string& s, const char lineend)
{   
    const size_t size = s.size();
    char* buf = new char[size + 1];
    size_t count = 0;
    for (size_t i = 0; i < size; ++i)
    {
        if( s[i] == lineend )  // filter the space lines
        {
            size_t first  = i;

            ++i;
            while(i < size)  // all space char
            {
                if( !std::isspace(s[i]))  break;
                ++i;
            }

            while(1)  // find last '/n'
            {
                if(s[--i] == lineend) 
                {
                    break;
                }
            }

            assert( first <= i);
            assert(s[i] == lineend);
        }

        buf[count] = s[i];
        ++count;
    }

    buf[count] = 0;
    std::string ret = std::string(buf);
    delete [] buf;

    return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值