替换文件内容(50分)

  • 主题发起人 主题发起人 ideafrog
  • 开始时间 开始时间
I

ideafrog

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位:<br>&nbsp; &nbsp; 我现在想对一文本文件,进行部分单词替换。<br>如文件中所有的you替换为i.<br><br>怎么进行?谢谢!
 
<br>function String_Replace(<br>&nbsp; OldSubString: string;<br>&nbsp; NewSubString: string;<br>&nbsp; SourceString: string): string;<br>var<br>&nbsp; P: Integer;<br>&nbsp; S: string;<br>&nbsp; R: string;<br>&nbsp; LOld: Integer;<br>&nbsp; LNew: Integer;<br>begin<br>&nbsp; S := SourceString;<br>&nbsp; R := '';<br>&nbsp; LOld := Length(OldSubString);<br>&nbsp; LNew := Length(NewSubString);<br>&nbsp; Result := S;<br>&nbsp; if OldSubString = '' then Exit;<br>&nbsp; if SourceString = '' then Exit;<br>&nbsp; P := Pos(OldSubString, S);<br>&nbsp; if P = 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; R := S;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; while P &lt;&gt; 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Delete(S, P, LOld);<br>&nbsp; &nbsp; &nbsp; R := R + Copy(S, 1, P - 1) + NewSubString;<br>&nbsp; &nbsp; &nbsp; S := Copy(S, P, Length(S) - (P - 1));<br>&nbsp; &nbsp; &nbsp; P := Pos(OldSubString, S);<br>&nbsp; &nbsp; &nbsp; if P = 0 then R := R + S;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; Result := R;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; f: textfile;<br>&nbsp; filename: string;<br>&nbsp; tempS: string;<br>&nbsp; strS: TStrings;<br>&nbsp; i: integer;<br>begin<br>&nbsp; filename := 'f:/temp/try.txt';<br>&nbsp; AssignFile(f, filename);<br>&nbsp; strS := TStringList.Create;<br>&nbsp; try<br>&nbsp; &nbsp; reset(f);<br>&nbsp; &nbsp; while not eof(f) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; tempS := '';<br>&nbsp; &nbsp; &nbsp; Readln(f, tempS);<br>&nbsp; &nbsp; &nbsp; strS.Add(String_Replace('you', 'i', tempS));<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CloseFile(f);<br>&nbsp; &nbsp; Rewrite(f);<br>&nbsp; &nbsp; for i := 0 to strS.Count - 1 do<br>&nbsp; &nbsp; &nbsp; Writeln(f, strS);<br>&nbsp; finally<br>&nbsp; &nbsp; CloseFile(f);<br>&nbsp; end;<br>end;<br>
 
用StringReplace
 
接受答案了.
 

Similar threads

后退
顶部