c++ String去除头尾空格

1.使用string的find_first_not_of,和find_last_not_of方法

 #include <iostream>
#include <string> std::string& trim(std::string &); int main()
{
std::string s = " Hello World!! ";
std::cout << s << " size:" << s.size() << std::endl;
std::cout << trim(s) << " size:" << trim(s).size() << std::endl; return ;
} std::string& trim(std::string &s)
{
if (s.empty())
{
return s;
} s.erase(,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + );
return s;
}
上一篇:Python-类的封装


下一篇:POJ-1251 Jungle Roads---MST裸题(需要编号)