C++常见问题: 字符串分割函数 split
C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速分割这个问题了。列几个常用方法以备不时之需。
方法一: 利用STL自己实现split 函数(常用,简单,直观)
原型: vector<string> split(const string &s, const string &seperator);
输入一个字符串,一个分隔符字符串(可包含多个分隔符),返回一个字符串向量。这是我最喜欢的方法,因为它最直观,在平常也最常用。实现及测试代码如下
#include <vector>
@egmkang 提供了一段更简洁高效的代码,实现如下:
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c) { std::string::size_type pos1, pos2; pos2 = s.find(c); pos1 = 0; while(std::string::npos != pos2) { v.push_back(s.substr(pos1, pos2-pos1)); pos1 = pos2 + c.size(); pos2 = s.find(c, pos1); } if(pos1 != s.length()) v.push_back(s.substr(pos1)); }
方法二: 用C语言中的strtok 函数来进行分割
原型: char *strtok(char *str, const char *delim);
strtok函数包含在头文件<string.h>中,对于字符数组可以采用这种方法处理。当然也可以将字符数组转换成字符串之后再使用法一。测试代码如下
#include <string.h>
方法三: boost库中包含了split 函数
boost库有很多方法来实现split,也包含了一个split函数,可以直接使用,非常实用而且强大,但是得自己下载boost库。使用代码如下
#include <boost/algorithm/string.hpp>
include <vector>
using namespace std;
using namespace boost;
void print( vector <string> & v )
{
for (size_t n = 0; n < v.size(); n++)
cout << “\”” << v[ n ] << “\”\n”;
cout << endl;
}
int main()
{
string s = “a,b, c ,,e,f,”;
vector <string> fields;
cout << “Original = \”” << s << “\”\n\n”;
cout << “Split on \’,\’ only\n”;
split( fields, s, is_any_of( ”,” ) );
print( fields );
cout << “Split on \” ,\”\n”;
split( fields, s, is_any_of( ” ,” ) );
print( fields );
cout << “Split on \” ,\” and elide delimiters\n”;
split( fields, s, is_any_of( ” ,” ), token_compress_on );
print( fields );
return 0;
}
输出结果如下:
Original = "a,b, c ,,e,f," Split on ',' only "a" "b" " c " "" "e" "f" "" Split on " ," "a" "b" "" "c" "" "" "e" "f" "" Split on " ," and elide delimiters "a" "b" "c" "e" "f" ""
在C++中还有很多方法来实现split 函数,cplusplus.com有个C++ split 专题,详细比较分析了几种实现方法(见下图)。链接见文末参考文献。
#———————————————————————————#
参考文献
《Accelerated C++》 by Andrew Koenig, Barbara E. Moo.
“Split a string” from cplusplus.com