您的位置:首页 > 其它

文本文件以指定的字符串分割

2008-06-12 08:59 218 查看
文本文件的分割。 这段代码是按照指定的分割符将文本文件分割成若干个片段。 代码如下:
unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type userarray=array of string;

type

TForm1 = class(TForm)

Edit1: TEdit;

Button1: TButton;

Button2: TButton;

ListBox1: TListBox;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

function split(s: string; dot: char): userarray;

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

uses StrUtils;

{$R *.dfm}

//按所给字符将字符串分隔成数组

function TForm1.split(s:string;dot:char):userarray;

var

str:userarray;

i,j:integer;

begin

i:=1;

j:=0;

SetLength(str, 255);

while Pos(dot, s) > 0 do     //Pos返回子串在父串中第一次出现的位置.

begin

str[j]:=copy(s,i,pos(dot,s)-i);

i:=pos(dot,s)+1;

s[i-1] := chr(ord(dot)+1);

j:=j+1;

end;

str[j]:=copy(s,i,strlen(pchar(s))-i+1);

result:=str;

end;

procedure TForm1.Button1Click(Sender: TObject);

var

ur:userarray;

i:Integer;

begin

memo1.Clear ;

ur:=split(Edit1.Text,',');

for i :=0 to 255 do

begin

if length(ur[i])=0 then Exit;

listbox1.Items.Add(ur[i]);

end;

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

close;

end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐