Python 中有一个非常有用的函数叫做 strip() 。 C++中有类似的吗?
原文由 MBZ 发布,翻译遵循 CC BY-SA 4.0 许可协议
我用这个:
#include <string>
#include <cctype>
std::string strip(const std::string &inpt)
{
auto start_it = inpt.begin();
auto end_it = inpt.rbegin();
while (std::isspace(*start_it))
++start_it;
while (std::isspace(*end_it))
++end_it;
return std::string(start_it, end_it.base());
}
原文由 Ferdi Kedef 发布,翻译遵循 CC BY-SA 4.0 许可协议
3 回答2k 阅读✓ 已解决
2 回答3.9k 阅读✓ 已解决
2 回答3.2k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
1 回答2.7k 阅读✓ 已解决
3 回答3.5k 阅读
3 回答497 阅读✓ 已解决
没有内置任何东西;我曾经使用类似以下的东西:
效果很好。 (我现在有一个更复杂的版本,可以正确处理 UTF-8。)