#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
char **funcSplit(char* iString, int* iCounter, char * iDelimiter)
{
char TmpStr[255];
char* TmpPointer;
char** TmpStrList = 0;
int TmpIdx = 0;
int TmpNOE = 0;
strcpy(TmpStr, iString);
TmpPointer = strtok(TmpStr, iDelimiter);
while (TmpPointer != 0 ) {
TmpStrList = (char**) realloc(TmpStrList, (TmpIdx + 1) * sizeof(char*));
TmpStrList[TmpIdx] = (char*) malloc((strlen(TmpPointer) + 1));
strcpy(TmpStrList[TmpIdx], TmpPointer);
++TmpIdx;
TmpPointer = strtok(NULL, iDelimiter);
}
*iCounter = TmpIdx;
return TmpStrList;
}
// Example
int _tmain(int argc, _TCHAR* argv[])
{
// char TmpStr[255] = "one, two, tree, four,five six;seven;eight, nine";
char *TmpStr = "one, two, tree, four,five six;seven;eight, nine";
int TmpCounter = 0;
int TmpIdx = 0;
char ** TmpStrList = funcSplit( TmpStr, &TmpCounter, " ,;");
for (TmpIdx = 0; TmpIdx < TmpCounter; ++TmpIdx)
printf("%s\n", TmpStrList[TmpIdx]);
for (TmpIdx = 0; TmpIdx < TmpCounter; ++TmpIdx)
free(TmpStrList[TmpIdx]);
free(TmpStrList);
getch();
return 0;
}
【Delphi】
function funcSplitStr(iSpliter: String; iSourceStr: String): TStrings;
var
TmpPos: Integer;
TmpStrList: TStringList;
TmpStr: String;
begin
TmpStrList := TStringList.Create;
TmpPos := pos(iSpliter, iSourceStr);
while (TmpPos > 0) do
begin
TmpStr := copy(iSourceStr, 1, (TmpPos - 1));
TmpStrList.Add(TmpStr);
iSourceStr := copy (iSourceStr , (TmpPos + Length(iSpliter)), (Length(iSourceStr) - TmpPos - Length(iSpliter) + 1));
TmpPos := pos(iSpliter, iSourceStr);
end ;
TmpStrList.Add(iSourceStr);
Result := TmpStrList;
end;
0 意見:
張貼留言