C++ string的trim, split方法

很多其他语言的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. }

  12. if (index-last>0)

  13. {

  14. ret->push_back(s.substr(last,index-last));

  15. }

  16.  

猜你喜欢

转载自blog.csdn.net/winnyrain/article/details/81170269