您的位置:首页 > 编程语言 > C语言/C++

C++如何实现string的trim功能? (已经包含trimLeft和trimRight)

2013-11-12 23:06 417 查看
      Just offer the code you need:

#include <string>
#include <iostream>
using namespace std;

string trim(string str)
{
if(string("") == str) // 不可少,否则有可能出错
{
return str;
}

string s = str.substr(str.find_first_not_of(" "));
return s.substr(0, s.find_last_not_of(" ") + 1);
}

int main()
{

string s = trim("   hello   world       ");

if(s == string("hello   world"))
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}

return 0;
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐