// splitstring.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#pragma warning(disable : 4786)
#include <vector>
#include <string>
//字符串分割函数
std::vector<std::string> split(std::string str,std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str+=pattern;//扩展字符串以方便操作
int size=str.size();
for(int i=0; i<size; i++)
{
pos=str.find(pattern,i);
if(pos<size)
{
std::string s=str.substr(i,pos-i);
result.push_back(s);
i=pos+pattern.size()-1;
}
}
return result;
}
int main(int argc, char* argv[])
{
std::vector<std::string> szArry;
szArry = split("good,asdf,asdf,as,dfas,df,asdf,as,dfas",",");
std::vector<std::string>::iterator index;
for (index = szArry.begin(); index != szArry.end(); index++)
{
printf("%s\n",(*index).c_str() );
}
return 0;
}
分割函数,是某个大侠写的,这里借用了一下!
vector<CString> split(CString str,CString pattern)
{
int pos;
vector<CString> result;
str+=pattern;//扩展字符串以方便操作
int size=str.GetLength();
for(int i=0; i<size; i++)
{
pos = str.Find(pattern,i);
if(pos<size)
{
CString s=str.Mid(i,pos-i);
result.push_back(s);
i = pos + pattern.GetLength() - 1;
}
}
return result;
}