C++拆分字符串

#include <vector>
#include <string>
#include <iostream>
using namespace std;

const vector<string> explode(const string& s, const char& c)
{
	string buff{""};
	vector<string> v;
	
	for(auto n:s)
	{
		if(n != c) buff+=n; else
		if(n == c && buff != "") { v.push_back(buff); buff = ""; }
	}
	if(buff != "") v.push_back(buff);
	
	return v;
}

int main()
{
	string str{"您好,欢迎开始,我们开始测试,了"};
	vector<string> v{explode(str, ',')};
	for(auto n:v)
	{
		cout << n << endl;
	} 
	
	return 0;
}

发布了343 篇原创文章 · 获赞 57 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/jadeshu/article/details/103776189