文本文件的转换,这里的转换是将文本文件中的特殊的标识符号或者说分隔符转换成自己比较熟悉的符号 就是简单的查找和替换,把比较生僻的符号转换成熟悉的,这样有利于在编程是分割字符串。 代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ReplaceTextA(var SText: string; TextFind, TextReplace: string);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ReplaceTextA(var SText: string; TextFind, TextReplace: string);
var
S: string;
SLen: LongInt;
SPos: Integer;
begin
S := '';
SLen := Length(TextFind);
SPos := Pos(TextFind, SText);
while SPos > 0 do
begin
S := S + Copy(SText, 1, SPos - 1) + TextReplace;
Delete(SText, 1, SPos + SLen - 1);
SPos := Pos(TextFind, SText);
end;
S := S + SText;
SText := S;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
i:integer;
begin
memo1.Clear;
memo2.Clear;
memo1.Lines.LoadFromFile('f:/b21.txt');
for i:=1 to memo1.Lines.Count do
begin
str:=memo1.Lines.Strings[i-1];
ReplaceTextA(str,',',', ');
memo2.Lines.Add(str);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
end.
调试的时候加两个Memo控件和两个按钮Button控件。