首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

delphi 在文本文件中平添和删除

2013-02-27 
delphi 在文本文件中添加和删除我想在固定的位置添加一行记录 if Pos(Str,Strtext) 0 then//我要在这

delphi 在文本文件中添加和删除
我想在固定的位置添加一行记录 
if Pos('Str',Strtext) > 0 then
//我要在这里添加一行

删除的时候也是
if Pos('Str',Strtext) > 0 then
//删除包含'Str'的这一行

这2个问题怎么做啊

[解决办法]
将Strtext: string;换为Strtext: TStringList;

然后将文本文件中的一行行读到Strtext中,用Strtext.Add();

在for循环Strtext,

添加:
i := 0;
while i < Strtext.Count do
begin
  if Pos('Str',Strtext[i]) > 0 then
  begin
    Strtext.Insert(i, '要插入的字符串');
    Inc(i);
  end;
  Inc(i);      
end;

删除:
i := 0;
while i < Strtext.Count do
begin
  if Pos('Str',Strtext[i]) > 0 then
    Strtext.Delete(i)
  else
    Inc(i);      
end;

热点排行