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

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

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

[cpp] view plaincopy

  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++标准库没有但是又非常常用和好用的库函数,例如正则表达式,线程库等等。

[cpp] view plaincopy

  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却可以)

[cpp] view plaincopy

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

[cpp] view plaincopy

  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. }
时间: 2024-08-29 21:59:41

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

java中String对象的split方法

在java.lang包中有String.split()方法,返回是一个String[]数组,今天碰到一个自己没注意的问题: 1.特殊分隔符 String str1 = "123|456|789"; System.out.println(str1.split("|")[0]); 结果是1 这里要注意的是"|"作为分隔符要写成这样 System.out.println(str1.split("\\|")[0]); 同理如果用&qu

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

split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj   必选项.要被分解的 String 对象或文字.该对象不会被 split 方法修改.separator 可选项.字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符.如果忽 略该选项,返回包含整个字符串的单一元素数组. limit可选项.该值用来限制返回数组中的元素个数. 说明:split 方法的结果

java基础----&gt;String中的split方法的原理

这里面主要介绍一下关于String类中的split方法的使用以及原理. split函数的说明 split函数java docs的说明: When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array.A zero-width match at the beg

String类的split()方法

在项目中用了"|"作为分割符,最后发现不能达到预期的效果; 原因:split()函数中的形参是作为一个正则表达式来使用的,而"|"对应的正则表达式不是|字符,若用使用需要转义 原文地址:https://www.cnblogs.com/zhaolei1996/p/12601124.html

关于String的split方法的一点疑问

今天莫名其妙的又做了一下leetcode上面的第一题,是简单的字符串逆序输出问题.下面是我处理的代码: public String reverseWords(String s) { if(s == null) return new String(""); StringBuffer fir = new StringBuffer(s); if(fir.length() == 0) return new String(""); while(fir.length() !=

string.split()与re.split()方法区别

re模块的split()方法与字符串的split()方法相似,前者是根据 正则表达式模式 分隔字符串,后者是根据 固定的字符串 分割,因此与后者相比,显著提升了字符分割的能力. 如果分隔符没有使用由特殊符号表示的正则表达式来匹配多个模式,那 re.split()和 string.split()的执行过程是一样的. string.split()与re.split()方法区别,布布扣,bubuko.com

String的split()方法探索和大揭秘

其实没打算写这么一篇博文的,但是昨天在逛论坛的时候,发现一帖子,然后我又把帖子的内容在群里发了一通,结果出现了让人惊讶的结果,所以这里简单的给大家分享一下split()方法,免得大伙儿以后还会出现这种基本知识错误! 接着说一下,昨天看到的帖子内容: String[] str1 = ";;;".split(";"); String[] str2 = ";a;;".split(";"); String[] str3 = "

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

split()首先是一个分隔符,它会把字符串按照split(' 字符')里的字符把字符串分割成数组,然后存给一个数组对象. 输出数组对象经常使用foreach或者for循环. 第一种方法 string s=abcdeabcdeabcde; string[] sArray=s.Split('c') ; foreach(string i in sArray) Console.WriteLine(i.ToString()); 输出下面的结果: ab deab deab de 第二种方法 我们看到了结果

java String.split方法是用注意点(转)

转自:http://www.blogjava.net/fanyingjie/archive/2010/08/05/328059.html 在java.lang包中有String.split()方法,返回是一个数组我在应用中用到一些,给大家总结一下,仅供大家参考: 1.如果用“.”作为分隔的话,必须是如下写法:String.split("\\."),这样才能正确的分隔开,不能用String.split("."); 2.如果用“|”作为分隔的话,必须是如下写法:Stri