C++ 实现 string split 的几种方法

方法1:string::find_first_of()

找到下一个token的位置,并利用substr
#include <string>
推荐使用这种方法,好用,而且灵活

void splitWithStringFind(vector<string> &ret, string str, string token)
{
	// str: "One, two,, four  , five,"
	// token: " ,"
	int cur, next = -1;
	do {
		cur = next + 1;
		next = str.find_first_of(token, cur);
		ret.push_back(str.substr(cur, next - cur));
	} while (next != string::npos);
	/*
	"One"
	""
	"two"
	""
	""
	"four"
	""
	""
	""
	"five"
	""
	*/
}

使用模板做一下封装,代码如下,可以直接copy使用。

// Encapsulate
struct AHaSplit {
	enum empties_t {empties_ok, no_empties};
};

template <typename Container>
Container& aHaSplit(
	Container& ret,
	const typename Container::value_type& str,
	const typename Container::value_type& token,
	AHaSplit::empties_t empties = AHaSplit::no_empties)
{
	ret.clear();
	size_t cur;
	size_t next = -1;
	do 
	{
		if (empties == AHaSplit::no_empties) {
			// 找到非token字符第一个位置
			next = str.find_first_not_of(token, next + 1);
			if (next == string::npos) break;
			next -= 1;
		}

		cur = next + 1;
		next = str.find_first_of(token, cur);
		ret.push_back(str.substr(cur, next - cur));

	} while (next != Container::value_type::npos);

	return ret;
}

方法2:getline

#include <sstream>
缺点是只能对单一字符划分

void splitWithGetline(vector<string> &ret, string str, char delimiter)
{
	// str: "One, two,, four  , five,"
	// delimiter: ' '
	istringstream ss(str);
	while (!ss.eof()) {
		string x;
		getline(ss, x, delimiter);
		ret.push_back(x);
	}
	/*
	"One"
	" two"
	""
	" four  "
	" five"
	""
	*/
}

方法3:Boost

#include <boost/algorithm/string.hpp>
需要安装好Boost

using namespace boost;
void splitWithBoost(vector<string> &ret, string str, string token)
{
	// str: "One, two,, four  , five,"
	// token: " ,"
	split(ret, str, is_any_of(token));
	/*
	"One"
	" two"
	""
	" four  "
	" five"
	""
	*/

	//split(ret, str, is_any_of(token), token_compress_on);
	/*
	"One"
	"two"
	"four"
	"five"
	""
	*/

	//split_regex(ret, str, regex(token));
}

方法4:strtok

C语言库函数strtok
#include<string.h>
使用strtok有很多问题,比如相邻分隔符之间看作空字段,末尾空字段省略等等,不推荐

详细可参考:http://www.cplusplus.com/faq/sequences/strings/split/

对话与讨论