字符串分割函数

                            procedure SplitString(s : string;Separator : char;Terms : TStringList);
{ This browses a string and divide it into terms whenever the given
  separator is found. The separators will be removed }
  var
  hs : string;
  p : integer;

begin
  Terms.Clear; // First remove all remaining terms
  if Length(s)=0 then   // Nothin' to separate
    Exit;
  p:=Pos(Separator,s);
  while P<>0 do
  begin
    hs:=Copy(s,1,p-1);   // Copy term
    Terms.Add(hs);       // Add to list
    Delete(s,1,p);       // Remove term and separator
    p:=Pos(Separator,s); // Search next separator
  end;
  if Length(s)>0 then
    Terms.Add(s);        // Add remaining term
end;

==========
= 用  法 
==========

var
Terms : TStringList;
i : integer;

const
TestStr = '1st term;2nd term;3rd term';

begin
  Terms:=TStringList.Create;
  SplitString(TestStr,';',Terms);
  for i:=0 to terms.Count-1 do
    ShowMessage(Terms.Strings[i]);
  Terms.Free;
end;



                                

查看回复