#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;
}
C++ split 字符串分割实现
最新推荐文章于 2022-05-27 00:34:50 发布