您的位置:首页 > 编程语言 > C语言/C++

C++ split 字符串分割实现

2015-03-25 13:59 651 查看
#include "stdafx.h"
#include <vector>
#include <Windows.h>
#include <iostream>
using namespace std;

vector<string> Split(const string &strContext, string strSpilt )
{
vector<string> vecStr;
int charLen = strSpilt.size();//分隔符的长度
int lastPos = 0;
int index = -1;
while ( -1 != ( index = strContext.find( strSpilt, lastPos ) ) )
{
vecStr.push_back( strContext.substr( lastPos, index - lastPos ) );
lastPos = index + charLen;
}
string lastString = strContext.substr( lastPos );//截取最后一个分隔符后的内容
if ( !lastString.empty() )
{//不为空也压进去
vecStr.push_back( lastString );
}
return vecStr;
}

int _tmain(int argc, _TCHAR* argv[])
{

vector<string> vecStr;
vecStr = Split( "aa,,,bbb,,,", ",,," );

system("pause");
return 0;
}


复制去Google翻译翻译结果
charLen
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: