Replacing a unicode character in UTF-8 file using delphi 2010

Posted by Jake Snake on Stack Overflow See other posts from Stack Overflow or by Jake Snake
Published on 2012-12-13T21:38:27Z Indexed on 2012/12/13 23:03 UTC
Read the original article Hit count: 258

Filed under:
|

I am trying to replace character (decimal value 197) in a UTF-8 file with character (decimal value 65)

I can load the file and put it in a string (may not need to do that though)

SS := TStringStream.Create(ParamStr1, TEncoding.UTF8);
SS.LoadFromFile(ParamStr1);
//S:= SS.DataString;
//ShowMessage(S);

However, how do i replace all 197's with a 65, and save it back out as UTF-8?

 SS.SaveToFile(ParamStr2);
 SS.Free;

-------------- EDIT ----------------

reader:= TStreamReader.Create(ParamStr1, TEncoding.UTF8);
 writer:= TStreamWriter.Create(ParamStr2, False, TEncoding.UTF8);

 while not Reader.EndOfStream do
 begin
  S:= reader.ReadLine;
  for I:= 1 to Length(S)  do
  begin
   if Ord(S[I]) = 350 then
   begin
    Delete(S,I,1);
    Insert('A',S,I);
   end;
  end;
  writer.Write(S + #13#10);
 end;

 writer.Free;
 reader.Free;

© Stack Overflow or respective owner

Related posts about delphi

Related posts about delphi-2010