Autor Beitrag
delphia1029
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 31.08.09 18:13 
Hi Leute, ich weiß dass es in der Hilfe dazu eine Stellungnahme gibt, komme aber nicht ganz dahinter.
Ich habe eine Textdatei, in der ich nach einen String suchen möchte und in austausche. In der Textdatei kommt der gesucht Begriff mehrmals vor, ess solle jeder ausgetauscht werden. Hier mein Quellcode:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
var lzd : TStringlist;
    a : String;
    u : Integer;

begin
lzd:=TStringList.Create;
lzd.LoadFromFile('C:\.....txt');
u := lzd.IndexOf('Haus');
if u <> -1 then
begin
lzd[u] := Stringreplace(lzd[u],'Haus',a,[rfReplaceAll]);
lzd.SaveToFile('C:\.....txt');
end;
lzd.free;

end;

a ... String ... Hütte

Moderiert von user profile iconGausi: Delphi-Tags hinzugefügt
Muck
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 98
Erhaltene Danke: 8

Win 8, Win 7, Vista, Win XP
Delphi XE3, Delphi 2009, Delphi 2007, Delphi 5
BeitragVerfasst: Mo 31.08.09 18:30 
Hi,

wenn ich sowas hatte, habe ich meistens so geloest, dass ich einfach den StringReplace auf der kompletten Datei laufen lasse. Sollte kein Problem sein, solange die Datei nicht 2 GB gross ist.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
var lzd : TStringlist; 
a : String
u : Integer; 
begin 
lzd:=TStringList.Create; 
lzd.LoadFromFile('C:\.....txt'); 
if Pos('Haus',lzd.text)>0 then begin
  lzd.text:=StringReplace(lzd.Text,'Haus','Huette',[rfReplaceAll]);
  lzd.SaveToFile('C:\.....txt'); 
end
lzd.free; 
end;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19341
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 31.08.09 20:34 
Wozu erst die if-Abfrage?
Und wo ist der Speicherschutzblock?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
var
  lzd: TStringList;
begin
  lzd := TStringList.Create;
  try
    lzd.LoadFromFile('C:\.....txt'); 
    lzd.Text := StringReplace(lzd.Text, 'Haus''Huette', [rfReplaceAll]);
    lzd.SaveToFile('C:\.....txt'); 
  finally
    lzd.Free;
  end;
end;
Muck
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 98
Erhaltene Danke: 8

Win 8, Win 7, Vista, Win XP
Delphi XE3, Delphi 2009, Delphi 2007, Delphi 5
BeitragVerfasst: Mo 31.08.09 21:05 
Hallo,

die IF Abfrage weil wir ja nicht eine Datei speichern wollen, die sich nicht geaendert hat, weil nichts veraendert worden ist. Ich habe das einfach mal so vom erste eintrag uebernommen. Daher auch kein Schutzblock. Natuerlich gehoert da einer hin.

bis denne