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

C/C++去空格的函数

2006-06-29 11:04 330 查看
经过测试不会疏漏任何一种空白字符,准确可靠

----------------------------------------C---------------------------------------
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char * trim(char * ptr)
{
int start,end,i;
if (ptr)
{
for(start=0; isspace(ptr[start]); start++)
;
for(end=strlen(ptr)-1; isspace(ptr[end]); end--)
;
for(i=start; i<=end; i++)
ptr[i-start]=ptr[i];
ptr[end-start+1]='/0';
return (ptr);
}
else
return NULL;
}

--------------------------------------C++-----------------------------------
#include <string>
using namespace std;

string trim(string &s)
{
const string &space =" /f/n/t/r/v" ;
string r=s.erase(s.find_last_not_of(space)+1);
return r.erase(0,r.find_first_not_of(space));
}

string ltrim(string &s)
{
const string &space =" /f/n/t/r/v" ;
return s.erase(0,s.find_first_not_of(space));
}

string rtrim(string &s)
{
const string &space =" /f/n/t/r/v" ;
return s.erase(s.find_last_not_of(space)+1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: