博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

分割字符串函数

Posted on 2008-11-11 17:24  YangHe  阅读(216)  评论(0)    收藏  举报

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

function SplitString(Source, Deli: string): TStringList; stdcall;

var
  EndOfCurrentString: byte;
  StringList: TStringList;
begin
  StringList := TStringList.Create;
  while Pos(Deli, Source) > 0 do
  begin
    EndOfCurrentString := Pos(Deli, Source);
    StringList.add(Copy(Source, 1, EndOfCurrentString - 1));
    Source := Copy(Source, EndOfCurrentString + Length(Deli), Length(Source) - EndOfCurrentString);
  end;
  Result := StringList;
  StringList.add(Source);
end;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  str,str1,str2:string;
  i:Integer;
  lst:TStringList;
begin
  str:='无抵|抗|sdfk|jfdk|f';
  str1:='|';
  lst:=SplitString(str,str1);
  for i:=0 to lst.Count -1  do
    ShowMessage(lst[i]);
  end;

end.