删除std :: string中的空格 [英] remove whitespace in std::string
本文介绍了删除std :: string中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++中,一个简单的方法是:
In C++, what's an easy way to turn:
这个std :: string
This std::string
\t\tHELLO WORLD\r\nHELLO\t\nWORLD \t
进入:
HELLOWORLDHELLOWORLD
推荐答案
std :: remove_if
和 std :: string :: erase
。
不完全安全的版本
s.erase( std::remove_if( s.begin(), s.end(), ::isspace ), s.end() );
对于更安全的版本,请替换 :: isspace
For safer version replace ::isspace
with
std::bind( std::isspace<char>, _1, std::locale::classic() )
(包含所有相关标题)
使用替换字符类型的版本用< char>
替换< ElementType>
是。你当然可以用一个不同的区域设置替换。
For a version that works with alternative character types replace <char>
with <ElementType>
or whatever your templated character type is. You can of course also replace the locale with a different one. If you do that, beware to avoid the inefficiency of recreating the locale facet too many times.
在C ++ 11中,您可以使更安全的版本成为一个lambda,其中包括:
In C++11 you can make the safer version into a lambda with:
[]( char ch ) { return std::isspace<char>( ch, std::locale::classic() ); }
这篇关于删除std :: string中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文