function SubStr(Str:string;const Position:integer;const Delimiter:string=','):string;
var
Strlen,ZIdx,SIdx,CPos:integer;
begin
Result:='';
Str:=Str+Delimiter;
StrLen:=Length(Str);
ZIdx:=1;
SIdx:=1;
while ZIdx<=StrLen do begin
CPos:=Pos(Delimiter,Str);
if CPos<>0 then begin
if SIdx=Position then begin
result:=Copy(Str,1,CPos-1);
break;
end;
delete(Str,1,CPos);
inc(SIdx);
end;
inc(ZIdx);
end;
end;
위 함수의 사용법은
var
str1: String;
str2: Array of String;
begin
str1 := '12345 ABCD 6 789 EF 한글나라';
procedure TForm1.BitBtn1Click(Sender: TObject);
var
TmpStr : String;
i : Integer;
begin
TmpStr := '12345 ABCD 6 789 EF 한글나라';
while Pos(' ', TmpStr) > 0 do
begin
i := Pos(' ', TmpStr);
ListBox1.Items.Add(Copy(TmpStr,1, i - 1));
TmpStr := Copy(TmpStr, I + 1, Length(TmpStr));
end;
ListBox1.Items.Add(TmpStr);
end;
type
TMyList = array[0..4] of String;
var
index : Integer;
TmpStr : String;
myList : TMylist;
begin
TmpStr:='12345 ABCD 6 789 EF 한글나라';
while True do begin
index := pos(' ' , TmpStr);
if index > 0 then begin
if myList[0] = '' then
myList[0] := copy(TmpStr, 0, index)
else if myList[1] = '' then
myList[1] := copy(TmpStr, 0, index)
else if myList[2] = '' then
myList[2] := copy(TmpStr, 0, index)
else if myList[3] = '' then
myList[3] := copy(TmpStr, 0, index)
else if myList[4] = '' then
myList[4] := copy(TmpStr, 0, index);
TmpStr := copy(TmpStr, index+1, length(TmpStr));
end else
break;
end;
var
strData :string;
begin
strData := '12345 ABCD 6 789 EF 한글나라';
ListBox1.Clear;
ExtractStrings([' '], [' '], PChar(strData), ListBox1.Items );
// ListBox1 안에 적당히 들어가 있음...
end;
또는
procedure TForm1.Button2Click(Sender: TObject);
var
tempList :TStringList;
strData :string;
begin
strData := '12345 ABCD 6 789 EF 한글나라';
tempList := TStringList.Create();
ExtractStrings([' '], [' '], PChar(strData), tempList );
// tempList 안에 들어 있으므로... 작업 하구난 다음....
// ...
FreeAndNil(tempList);
end;