将文本文件加到memo中,作替换后存盘

解决方案 »

  1.   

    用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;
     
     
      

  2.   

    function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
      

  3.   

    呵呵,自己编程序吧,使用RichEdit控件,
     i:=Richedit1.FindText(文本,起始位置,长度,[stMatchCase]);
    i返回结果就是该字符串的起始位置,然后你在编程替换,用copy,如果要考虑所有情况,还是比较复杂的
      

  4.   

    对了,你可以使用chiro的方法,写文件可以考虑我的方法。
    ////////////////以下有错,对不起/////////////////
    mFS.Write(PChar(Result)^,Length(Result));//保存文件
    改为
    mFS.Write(PChar(RS)^,Length(RS));
    ///////////////////////////////////////
    Really Sorry!