用Pos和Copy配合使用。 eg: function ReplaceWordFromFile(....):String; var mFs:TFileStream; mOldWord:String; mNewWord:String; S,RS:String; Begin mFS:=TFileStream.Create(YourFileName,fmOpenRead);//Open the File SetLength(S,mFS.Size);//Important mFS.Read(PChar(S)^,mFS.Size);//Type Cast and Read the Content RS:=Copy(S,1,Pos(mOldWord,S)-1); RS:=RS+mNewWord; RS:=RS+Copy(S,Pos(mOldWord,S)+Length(mOldWord),Length(S)-Pos(mOldWord,S)+Length(mOldWord)+1); mFs.Free; mFS:=TFileStream.Create(YourFileName,fmCreate); mFS.Write(PChar(Result)^,Length(Result));//保存文件 mFS.Free; end;
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
eg:
function ReplaceWordFromFile(....):String;
var
mFs:TFileStream;
mOldWord:String;
mNewWord:String;
S,RS:String;
Begin
mFS:=TFileStream.Create(YourFileName,fmOpenRead);//Open the File
SetLength(S,mFS.Size);//Important
mFS.Read(PChar(S)^,mFS.Size);//Type Cast and Read the Content
RS:=Copy(S,1,Pos(mOldWord,S)-1);
RS:=RS+mNewWord;
RS:=RS+Copy(S,Pos(mOldWord,S)+Length(mOldWord),Length(S)-Pos(mOldWord,S)+Length(mOldWord)+1);
mFs.Free;
mFS:=TFileStream.Create(YourFileName,fmCreate);
mFS.Write(PChar(Result)^,Length(Result));//保存文件
mFS.Free;
end;
i:=Richedit1.FindText(文本,起始位置,长度,[stMatchCase]);
i返回结果就是该字符串的起始位置,然后你在编程替换,用copy,如果要考虑所有情况,还是比较复杂的
////////////////以下有错,对不起/////////////////
mFS.Write(PChar(Result)^,Length(Result));//保存文件
改为
mFS.Write(PChar(RS)^,Length(RS));
///////////////////////////////////////
Really Sorry!