procedure StrToStrings(S: AnsiString; Sep: AnsiString; const List: TStrings);
var
I, L: Integer;
Left: AnsiString;
begin
Assert(List <> nil);
List.Clear;
L := Length(Sep);
I := Pos(Sep, S);
while (I > 0) do
begin
Left := StrLeft(S, I - 1);
List.Add(Left);
Delete(S, 1, I + L - 1);
I := Pos(Sep, S);
end;
if S <> '' then
List.Add(S);
end;
var
I, L: Integer;
Left: AnsiString;
begin
Assert(List <> nil);
List.Clear;
L := Length(Sep);
I := Pos(Sep, S);
while (I > 0) do
begin
Left := StrLeft(S, I - 1);
List.Add(Left);
Delete(S, 1, I + L - 1);
I := Pos(Sep, S);
end;
if S <> '' then
List.Add(S);
end;
原理很简单,你看看就知道!
num初值为1,是因为考虑到你可能输入
'abc,dkkd,dkkd,'最后逗号就没了的情况!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
type type1=array of string;
{$R *.dfm}
function Split(str:string;seperator:char;var hh:integer):type1;
var i:integer;
var num:integer;
begin
num:=1;
for i:=1 to Length(str) do
begin
if str[i]=seperator then
begin
num:=num+1;
end;
end;
hh:=num;
setlength(result,num);
num:=0;
for i:=1 to length(str) do
begin
if str[i]=seperator then
begin
num:=num+1;
continue;
end;
Result[num]:=Result[num]+str[i];
end;end;
procedure TForm1.Button1Click(Sender: TObject);
var p:type1;
var k:integer;
var i:integer;
begin
p:=split('adddd,bddd,c,dfjdkf,',',',k);
showMessage(inttostr(k));
for i:=0 to k-1 do
begin
ShowMessage(p[i]);
end;
end;end.