TStringsEncoding TEncoding

TStringsEncoding (Delphi)

Language:

Versions:

Description

This example requires a TMemo control, two buttons and two textfields on the form. It demonstrates the new TStrings.Encoding property and the overloads of TStrings.LoadFromFile and SaveToFile that use it. Use this example to save lines in a specific encoding to a file and then load the file back in, reading the encoding. Notice that the ASCII format does not preserve wide characters.

提供了两种保存文件的方法

    virtual void __fastcall SaveToFile(const System::UnicodeString FileName)/* overload */;
    virtual void __fastcall SaveToFile(const System::UnicodeString FileName, System::Sysutils::TEncoding* Encoding)/* overload */;


Code

procedure TForm1.Button1Click(Sender: TObject);
var
  // You may need to change this path to suit your environment.
  Path: String;
begin
  Path:= Edit1.Text;
  Memo1.Lines.LoadFromFile(Path); // The encoding is read from the BOM and stored in the Encoding property.
  if Memo1.Lines.Encoding <> nil then
    Edit2.Text := Memo1.Lines.Encoding.EncodingName;
  Memo1.ScrollBars := ssVertical;
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
  // You may need to change this path to suit your environment.
  Path: String;
  myEncoding : TEncoding;
begin
  Path:= Edit1.Text;
  myEncoding := TEncoding.Default;
  if (Edit2.Text = 'ASCII') then myEncoding := TEncoding.ASCII;
  if (Edit2.Text = 'BigEndianUnicode') then myEncoding := TEncoding.BigEndianUnicode;
  if (Edit2.Text = 'Default') then myEncoding := TEncoding.Default;
  if (Edit2.Text = 'Unicode') then myEncoding := TEncoding.Unicode;
// Do not use UTF7 for this.  It does not have a BOM, and so the encoding cannot be detected on a load.
//  if (Edit2.Text = 'UTF7') then myEncoding := TEncoding.UTF7;
  if (Edit2.Text = 'UTF8') then myEncoding := TEncoding.UTF8;
  Memo1.Lines.SaveToFile(Path, myEncoding);
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值