码迷,mamicode.com
首页 > 编程语言 > 详细

[转]C++ string的trim, split方法

时间:2015-06-18 16:47:22      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

很多其他语言的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.   
  10. std::string& trim(std::string &);  
  11.   
  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.   
  18.     return 0;  
  19. }  
  20.   
  21. std::string& trim(std::string &s)   
  22. {  
  23.     if (s.empty())   
  24.     {  
  25.         return s;  
  26.     }  
  27.   
  28.     s.erase(0,s.find_first_not_of(" "));  
  29.     s.erase(s.find_last_not_of(" ") + 1);  
  30.     return s;  
  31. }  

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.   
  11. using namespace std;  
  12. using namespace boost;  
  13.   
  14. int main() {  
  15.   string s = " hello boost!! ";  
  16.   trim(s);  
  17.   cout << s << endl;  
  18. }  

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.   
  11. template <class T>  
  12. std::basic_string<T>& trim(std::basic_string<T>&);  
  13.   
  14. int main( )   
  15. {  
  16.     std::string s = " Hello World!! ";  
  17.     std::cout << s << " size:" << s.size() << std::endl;  
  18.     std::cout << trim(s) << " size:" << trim(s).size() << std::endl;  
  19.   
  20.     return 0;  
  21. }  
  22.   
  23. template <class T>  
  24. std::basic_string<T>& trim(std::basic_string<T>& s)   
  25. {  
  26.     if (s.empty()) {  
  27.         return s;  
  28.   }  
  29.   
  30.     std::basic_string<T>::iterator c;  
  31.     // Erase whitespace before the string  
  32.   
  33.     for (c = s.begin(); c != s.end() && iswspace(*c++);); s.erase(s.begin(), --c);  
  34.   
  35.     // Erase whitespace after the string  
  36.   
  37.     for (c = s.end(); c != s.begin() && iswspace(*--c);); s.erase(++c, s.end());  
  38.   
  39.     return s;  
  40. }  


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方法

标签:

原文地址:http://www.cnblogs.com/wxmdevelop/p/4586168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!