试试这个:
rocedure TForm1.Button1Click(Sender: TObject);
var
list : Tstringlist;
i,count: integer;
s : string;
k : string;
begin
try
list :=Tstringlist.Create;
list.LoadFromFile('c:/1.txt');
s:=list.Text;
i:=length(s);
k:='';
count:=1;
while count<i do
begin
if s[count]=' ' then
begin
k:=k+'|';
inc(count);
while s[count]=' ' do
inc(count);
continue;
end;
k:=k+s[count];
inc(count);
end;
list.Clear;
list.Text:=k;
list.SaveToFile('c:/1.txt');
finally
freeandnil(list);
end;
end;
function ReplaceChar(Const S:String):String ;
var oldc:char;
i,len,ct:integer;
begin
oldc:=#0;
len:=length(S);
if len=0 then
exit;
ct:=0;
i:=1;
while (i<=len) and (S[i]=' ') do
inc(i);
if i>len then
exit;
SetLength(ReSult,Len);
while i<=len do
begin
if S[i]<>' ' then
begin
inc(ct);
if oldc=' ' then
begin
ReSult[ct]:='|';
inc(ct);
end;
ReSult[ct]:=S[i];
end;
oldc:=S[i];
inc(i);
end;
Setlength(ReSult,ct);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
list : Tstringlist;
i,j : integer;
s : string;
k : string;
begin
try
list :=Tstringlist.Create;
list.Clear;
list.LoadFromFile('c:/1.txt');
for i:=0 to list.Count-1 do
begin
for j:=0 to length(list[i])-1 do
begin
s:=list[i];
if s[j]=' ' then
k:=k+''+'|'+''
else
k:=k+s[j]; ;
end;
s:=k;
list[i]:=s;
end;
list.SaveToFile('c:/1.txt');
finally
freeandnil(list);
end;
end;