【C++】通用函数:过滤string的空行
需要过滤string中的空行,可以网上搜索未果。最终自己写了一个较为通用的函数。 输入s表示要过滤的string;lineend表示行结束符,不同平台的行结束符可能不同。WIn和linux是nl/lf(/n), mac平台则以cr(/r)结尾。 std::string DeleteSpaceLineOfStr(const std::string& s, const char l
需要过滤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;
}
更多推荐
所有评论(0)