C++ 删除 std::string 中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14233065/
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
remove whitespace in std::string
提问by Mr. Smith
In C++, what's an easy way to turn:
在 C++ 中,有什么简单的方法可以转换:
This std::string
这个 std::string
\t\tHELLO WORLD\r\nHELLO\t\nWORLD \t
Into:
进入:
HELLOWORLDHELLOWORLD
回答by CashCow
Simple combination of std::remove_if
and std::string::erase
.
的简单组合std::remove_if
和std::string::erase
。
Not totally safe version
不是完全安全的版本
s.erase( std::remove_if( s.begin(), s.end(), ::isspace ), s.end() );
For safer version replace ::isspace
with
为了更安全的版本替换::isspace
为
std::bind( std::isspace<char>, _1, std::locale::classic() )
(Include all relevant headers)
(包括所有相关标题)
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.
对于使用替代字符类型的版本,请替换<char>
为<ElementType>
您的模板化字符类型。您当然也可以用不同的语言环境替换语言环境。如果这样做,请注意避免多次重新创建语言环境方面的低效率。
In C++11 you can make the safer version into a lambda with:
在 C++11 中,您可以使用以下命令将更安全的版本转换为 lambda:
[]( char ch ) { return std::isspace<char>( ch, std::locale::classic() ); }
回答by billz
If C++03
如果 C++03
struct RemoveDelimiter
{
bool operator()(char c)
{
return (c =='\r' || c =='\t' || c == ' ' || c == '\n');
}
};
std::string s("\t\tHELLO WORLD\r\nHELLO\t\nWORLD \t");
s.erase( std::remove_if( s.begin(), s.end(), RemoveDelimiter()), s.end());
Or use C++11 lambda
或者使用 C++11 lambda
s.erase(std::remove_if( s.begin(), s.end(),
[](char c){ return (c =='\r' || c =='\t' || c == ' ' || c == '\n');}), s.end() );
PS. Erase-remove idiomis used
附注。擦除remove惯用法使用
回答by locojay
c++11
C++11
std::string input = "\t\tHELLO WORLD\r\nHELLO\t\nWORLD \t";
auto rs = std::regex_replace(input,std::regex("\s+"), "");
std::cout << rs << std::endl;
/tmp ??? ./play
/tmp ???。/玩
HELLOWORLDHELLOWORLD
回答by pje
In C++11 you can use a lambda rather than using std::bind:
在 C++11 中,您可以使用 lambda 而不是使用 std::bind:
str.erase(
std::remove_if(str.begin(), str.end(),
[](char c) -> bool
{
return std::isspace<char>(c, std::locale::classic());
}),
str.end());
回答by TemplateRex
You could use Boost.Algorithm's erase_all
你可以使用Boost.Algorithm的erase_all
#include <boost/algorithm/string/erase.hpp>
#include <iostream>
#include <string>
int main()
{
std::string s = "Hello World!";
// or the more expensive one-liner in case your string is const
// std::cout << boost::algorithm::erase_all_copy(s, " ") << "\n";
boost::algorithm::erase_all(s, " ");
std::cout << s << "\n";
}
NOTE: as is mentioned in the comments: trim_copy
(or its cousins trim_copy_left
and trim_copy_right
) only remove whitespace from the beginning and end of a string.
注意:正如评论中提到的:(trim_copy
或其表亲trim_copy_left
和trim_copy_right
)仅从字符串的开头和结尾删除空格。
回答by SelectricSimian
Stepping through it character by character and using string::erase()
should work fine.
逐个字符地遍历它并使用它string::erase()
应该可以正常工作。
void removeWhitespace(std::string& str) {
for (size_t i = 0; i < str.length(); i++) {
if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {
str.erase(i, 1);
i--;
}
}
}