c++ string类型 去头尾空格

来源:百度知道 编辑:UC知道 时间:2024/05/26 09:15:17
不要和我说用trim() 这个是不行的,
帮我写个去string头尾空格的,拿来调用的子函数。
先谢谢了!
写的简单的加分。

给你提供了一个remove_space(string& str)函数,把要去掉空格的串str传入函数即可,函数返回后,str中的内容即被前后去除了多余的空格。 不明白的地方可以hi我
#include<string>
using namespace std;
void remove_space(string& str){
string buff(str);
char space = ' ';
str.assign(buff.begin() + buff.find_first_not_of(space),
buff.begin() + buff.find_last_not_of(space) + 1);
}

测试:
(1)前后有若干个空格的情况,输入" abc ",输出:"abc"
(2)前后有若干空格,且字符串中间也夹杂着若干空格的情况,输入" a b cd ", 输出:"a b cd"

相信这就是你要的

#include <string>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

inline string& lTrim(string &ss)
{
string::iterator p=find_if(ss.begin(),ss.end(),not1(ptr_fun(isspace)));
ss.erase(ss.begin(),p);
return ss;
}

inline string& rTrim(string &ss)
{