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); }
相关文章推荐
- 写一个函数,将其中的/t都转换成4个空格。(C++版)
- C++的string前后去空格函数Trim
- c++经典题目:写个函数,将其中的\t都转换成4个空格
- c++ 去除字符串中的空格和标点符号 (remove_if 函数的用法)
- C++怎么让函数返回固定长度字符串,比如给“zhangsan"返回原字符串加上4各空格,给“lisi"返回"lisi"加6
- C/C++ 去掉字符串首尾空格函数
- C++中的字符串的输入输出函数、空字符与空格的区别、字符变量赋值和字符串初始化
- C/C++去空格的函数
- c++ 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
- C/C++ 去掉字符串首尾空格函数
- C++时间函数
- C++里的seekg(),tellg(),seekp(),tellp()函数
- C++之函数指针
- C++ list函数详解
- C/C++字符串转换到数字的函数
- 2014秋C++ 第11周项目 函数及其应用
- 第2周项目1c/c++语言中函数参数传递的三种方式
- C++ 求排列函数 next_permutation, prev_permutation
- C++ 重新定义继承而来的非虚函数
- C/C++时间处理函数