c++分割字符串
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
using namespace std;
int ParseString( vector<string>& vecSave, const char* pszSrc, const char* pszSplit )
{
assert( NULL != pszSrc );
assert( NULL != pszSplit );
string strSrc = pszSrc;
unsigned int nSplitLen = strlen( pszSplit );
while( strSrc.size() > 0 )
{
unsigned int n = strSrc.find( pszSplit );
if( string::npos == n )
{
vecSave.push_back( strSrc );
break;
}
if( 0 == n )
{
vecSave.push_back( "" );
continue;
}
vecSave.push_back( strSrc.substr( 0, n ) );
strSrc.erase( 0, n + nSplitLen );
}
return vecSave.size();
}
int main( int argc, char** argv )
{
string str = "zhang_yang_hua";
vector<string> vec;
ParseString( vec, str.data(), "_" );
for( vector<string>::iterator itr = vec.begin(); itr!=vec.end(); itr++ )
{
cout<<itr->data()<<endl;
}
return 0;
}
lua分割字符串
function _ParseString( strTemp, strSplit )
--local strTemp = "1;2;3;4;5"
--local strSplit = ";"
local nBegin = 1
local nStart = 0
local nEnd = 0
local arr = {}
LogInfo("len:" .. string.len(strTemp))
while string.len(strTemp) >= nBegin do
nStart,nEnd = strTemp.find( strTemp, strSplit, nBegin )
if nil == nStart then
break
end
local str = string.sub( strTemp, nBegin, nStart-1 )
if nil ~= str and string.len(str)>0 then
arr[table.getn(arr)+1] = str
end
nBegin = nEnd + 1
end
if nBegin <= string.len(strTemp) then
arr[table.getn(arr)+1] = string.sub( strTemp, nBegin, -1 )
end
local n = 1
while n<= table.getn(arr) do
LogInfo( "arr[" .. n .. "]:" .. arr[n] )
n = n + 1
end
end