很多其他语言的libary都会有去除string类的首尾空格的库函数,但是标准C++的库却不提供这个功能。但是C++string也提供很强大的功能,实现trim这种功能也不难。下面是几种方法:

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

  1. /*
  2. Filename : StringTrim1.cpp
  3. Compiler : Visual C++ 8.0
  4. Description : Demo how to trim string by find_first_not_of & find_last_not_of
  5. Release : 11/17/2006
  6. */
  7. #include <iostream>
  8. #include <string>
  9. std::string& trim(std::string &);
  10. int main()
  11. {
  12. std::string s = " Hello World!! ";
  13. std::cout << s << " size:" << s.size() << std::endl;
  14. std::cout << trim(s) << " size:" << trim(s).size() << std::endl;
  15. return 0;
  16. }
  17. std::string& trim(std::string &s)
  18. {
  19. if (s.empty())
  20. {
  21. return s;
  22. }
  23. s.erase(0,s.find_first_not_of(" "));
  24. s.erase(s.find_last_not_of(" ") + 1);
  25. return s;
  26. }

2.使用boost库中的trim,boost库对提供很多C++标准库没有但是又非常常用和好用的库函数,例如正则表达式,线程库等等。

  1. /*
  2. Filename : boostStringTrim.cpp
  3. Compiler : Visual C++ 8.0 / ISO C++ (boost)
  4. Description : Demo how to boost to trim string
  5. Release : 02/22/2007 1.0
  6. */
  7. #include <iostream>
  8. #include <string>
  9. #include <boost/algorithm/string.hpp>
  10. using namespace std;
  11. using namespace boost;
  12. int main() {
  13. string s = " hello boost!! ";
  14. trim(s);
  15. cout << s << endl;
  16. }

3.使用template(我用GCC编译不通过,用VS2005却可以)

  1. /*
  2. Filename : stringTrim1.cpp
  3. Compiler : Visual C++ 8.0
  4. Description : Demo how to trim string by other method.
  5. Release : 11/18/2006
  6. */
  7. #include <string>
  8. #include <iostream>
  9. #include <cwctype>
  10. template <class T>
  11. std::basic_string<T>& trim(std::basic_string<T>&);
  12. int main( )
  13. {
  14. std::string s = " Hello World!! ";
  15. std::cout << s << " size:" << s.size() << std::endl;
  16. std::cout << trim(s) << " size:" << trim(s).size() << std::endl;
  17. return 0;
  18. }
  19. template <class T>
  20. std::basic_string<T>& trim(std::basic_string<T>& s)
  21. {
  22. if (s.empty()) {
  23. return s;
  24. }
  25. std::basic_string<T>::iterator c;
  26. // Erase whitespace before the string
  27. for (c = s.begin(); c != s.end() && iswspace(*c++);); s.erase(s.begin(), --c);
  28. // Erase whitespace after the string
  29. for (c = s.end(); c != s.begin() && iswspace(*--c);); s.erase(++c, s.end());
  30. return s;
  31. }

split方法

  1. //注意:当字符串为空时,也会返回一个空字符串
  2. void split(std::string& s, std::string& delim,std::vector< std::string >* ret)
  3. {
  4. size_t last = 0;
  5. size_t index=s.find_first_of(delim,last);
  6. while (index!=std::string::npos)
  7. {
  8. ret->push_back(s.substr(last,index-last));
  9. last=index+1;
  10. index=s.find_first_of(delim,last);
  11. }

[转]C++ string的trim, split方法的更多相关文章

  1. String类的split方法以及StringTokenizer

    split方法可以根据指定的表达式regex将一个字符串分割成一个子字符串数组. 它的参数有两种形式,也即:split(String regex)和split(String regex, int li ...

  2. java中String对象的split方法

    在java.lang包中有String.split()方法,返回是一个String[]数组,今天碰到一个自己没注意的问题: 1.特殊分隔符 String str1 = "123|456|78 ...

  3. java基础---->String中的split方法的原理

    这里面主要介绍一下关于String类中的split方法的使用以及原理. split函数的说明 split函数java docs的说明: When there is a positive-width m ...

  4. 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!

    split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj   必选项.要被分解的 ...

  5. JAVA中string类的split方法

    split([separator,[limit]])第一个参数为分隔符,可以是一个正则表达式,第二个参数为返回结果数组的长度

  6. String.split()方法你可能不知道的一面

    一.问题 java中String的split()是我们经常使用的方法,用来按照特定字符分割字符串,那么我们看以下一段代码: public void splitTest() { String str = ...

  7. String的split方法,你真的懂吗

    String的split方法相信大家都不陌生,或多或少都用过它将字符串转成一个数组,但是就是这样一个简单的方法,里面也有一个不得不注意.不深不浅的小坑. 本地测试代码如下图所示: 图1 大家会发现sp ...

  8. 自己实现String.prototype.trim方法

    今天呢 知乎看到一道题 说是网易面试题,要求自己写一个trim()方法, 实现 var str = "   a   sd  "; 去掉字符串两端的空格. 直接上码 var str ...

  9. C# String.split()用法小结。String.Split 方法 (String[], StringSplitOptions)

    split()首先是一个分隔符,它会把字符串按照split(' 字符')里的字符把字符串分割成数组,然后存给一个数组对象. 输出数组对象经常使用foreach或者for循环. 第一种方法 string ...

随机推荐

  1. HTML <img> 标签的 height 和 width 属性

    定义和用法 <img> 标签的 height 和 width 属性设置图像的尺寸. 提示:为图像指定 height 和 width 属性是一个好习惯.如果设置了这些属性,就可以在页面加载时 ...

  2. android 自定义弹出框AlertDialog ,很炫的哦

      于是就小小的模仿了下自己写了这个这样的效果,主要代码如下:dlg = new AlertDialog.Builder(context).create();dlg.show();dlg.getWin ...

  3. GPS基础

    public class MainActivity extends Activity { private LocationManager manager; private List<String ...

  4. Linux常用命令_(文件搜索)

    文件查找主要包含以下几个命令 which.whereis.grep.find.wc

  5. 直接操作游戏对象C#游戏开发

    直接操作游戏对象C#游戏开发 2.2.3  直接操作游戏对象 在Inspector视图里通过设置属性而改变游戏场景中游戏对象的状态,太过抽象,毕竟数字并不够直观.其实,改变游戏对象的状态,完全有最最直 ...

  6. Ajax本地跨域问题

    问题:打开本地html文件时,,报错如下 Cross origin requests are only supported for protocol schemes: http, data,chrom ...

  7. 看病要排队(stl)

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  8. DP(优化) UVALive 6073 Math Magic

    /************************************************ * Author :Running_Time * Created Time :2015/10/28 ...

  9. 水题 ZOJ 3869 Ace of Aces

    题目传送门 水题,找出出现次数最多的数字,若多个输出Nobody //#include <bits/stdc++.h> //using namespace std; #include &l ...

  10. BZOJ4347 : [POI2016]Nim z utrudnieniem

    将石子从小到大排序,然后DP. 设$f[i][j][k]$表示考虑了前$i$堆的石子,当前扔掉的堆数模$d$为$j$,没有扔掉的石子的异或和为$k$的方案数. 因为石子排过序,所以转移的复杂度为$O( ...