本文实例讲述了c++ String去除头尾空格的方法,分享给大家供大家参考。具体实现方法如下:
实现该功能可使用string的find_first_not_of,和find_last_not_of方法,具体实现带如下:
std::string& trim(std::string &);
int main() return 0; std::string& trim(std::string &s) s.erase(0,s.find_first_not_of(" "));
{
std::string s = " Hello World!! ";
std::cout <
std::cout <
}
{
if (s.empty())
{
return s;
}
s.erase(s.find_last_not_of(" ") + 1);
return s;
}
希望本文所述对大家的C++程序设计有所帮助。