我有这段代码,但它输出
Hello World
不带下划线。如果我想输出Hello_World
,我该怎么做?
string s = "Hello_World 1 2";
s.erase(remove_if(s.begin(), s.end(), [](char c) { return !isalpha(c); } ), s.end());
最佳答案
您可以通过向 std::remove_if
函数添加另一个条件来实现特定输出。
std::string s = "Hello_World 1 2";
s.erase( std::remove_if(s.begin(), s.end(), [](char c)
{
return !isalpha(c) && c != '_';
}),
s.end());
std::cout << s;
关于c++ - 在 C++ 中从字符串中删除数字并保留下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50418126/