Delphi 2009 中 string 与 Char 的改变

在 Delphi 2009 中:

string = UnicodeString; (同样: PString = PUnicodeString;)

Char = WideChar; (同样: PChar = PWideChar;)



本例效果图:

Delphi 2009 中 string 与 Char 的改变

代码文件:

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Memo1: TMemo;

    Edit1: TEdit;

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    Button4: TButton;

    Button5: TButton;

    Button6: TButton;

    Button7: TButton;

    procedure FormCreate(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure Button4Click(Sender: TObject);

    procedure Button5Click(Sender: TObject);

    procedure Button6Click(Sender: TObject);

    procedure Button7Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



procedure TForm1.FormCreate(Sender: TObject);

begin

  Memo1.Clear;

  Edit1.Alignment := taCenter;

  Edit1.Text := '''万一的 Delphi 博客''';

  Button1.Caption := 'string';

  Button2.Caption := 'AnsiString';

  Button3.Caption := 'WideString';

  Button4.Caption := 'Char';

  Button5.Caption := 'AnsiChar';

  Button6.Caption := 'WideChar';

  Button7.Caption := '清' + #10#10 + '空';

  Button7.WordWrap := True;

end;



procedure TForm1.Button1Click(Sender: TObject);

var

  str: string;

  i: Integer;

begin

  str := Edit1.Text;

  Memo1.Clear;

  for i := 1 to Length(str) do Memo1.Lines.Add(str[i]);

end;



procedure TForm1.Button2Click(Sender: TObject);

var

  str: AnsiString;

  i: Integer;

begin

  str := Edit1.Text;

  Memo1.Clear;

  for i := 1 to Length(str) do Memo1.Lines.Add(str[i]);

end;



procedure TForm1.Button3Click(Sender: TObject);

var

  str: WideString;

  i: Integer;

begin

  str := Edit1.Text;

  Memo1.Clear;

  for i := 1 to Length(str) do Memo1.Lines.Add(str[i]);

end;



procedure TForm1.Button4Click(Sender: TObject);

var

  c: Char;

begin

  Memo1.Clear;

  for c in Edit1.Text do Memo1.Lines.Add(c);

end;



procedure TForm1.Button5Click(Sender: TObject);

var

  c: AnsiChar;

begin

  Memo1.Clear;

  for c in AnsiString(Edit1.Text) do Memo1.Lines.Add(c);

end;



procedure TForm1.Button6Click(Sender: TObject);

var

  c: WideChar;

begin

  Memo1.Clear;

  for c in Edit1.Text do Memo1.Lines.Add(c);

end;



procedure TForm1.Button7Click(Sender: TObject);

begin

  Memo1.Clear;

end;



end.


 
   
窗体文件:

object Form1: TForm1

  Left = 0

  Top = 0

  Caption = 'Form1'

  ClientHeight = 223

  ClientWidth = 256

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'Tahoma'

  Font.Style = []

  OldCreateOrder = False

  OnCreate = FormCreate

  PixelsPerInch = 96

  TextHeight = 13

  object Memo1: TMemo

    Left = 0

    Top = 0

    Width = 73

    Height = 223

    Align = alLeft

    Font.Charset = DEFAULT_CHARSET

    Font.Color = clWindowText

    Font.Height = -11

    Font.Name = 'Tahoma'

    Font.Style = []

    Lines.Strings = (

      'Memo1')

    ParentFont = False

    TabOrder = 0

    ExplicitHeight = 174

  end

  object Edit1: TEdit

    Left = 79

    Top = 11

    Width = 170

    Height = 21

    TabOrder = 1

    Text = 'Edit1'

  end

  object Button1: TButton

    Left = 79

    Top = 38

    Width = 121

    Height = 25

    Caption = 'Button1'

    TabOrder = 2

    OnClick = Button1Click

  end

  object Button2: TButton

    Left = 79

    Top = 69

    Width = 121

    Height = 25

    Caption = 'Button2'

    TabOrder = 3

    OnClick = Button2Click

  end

  object Button3: TButton

    Left = 79

    Top = 100

    Width = 121

    Height = 25

    Caption = 'Button3'

    TabOrder = 4

    OnClick = Button3Click

  end

  object Button4: TButton

    Left = 79

    Top = 131

    Width = 121

    Height = 25

    Caption = 'Button4'

    TabOrder = 5

    OnClick = Button4Click

  end

  object Button5: TButton

    Left = 79

    Top = 162

    Width = 121

    Height = 25

    Caption = 'Button5'

    TabOrder = 6

    OnClick = Button5Click

  end

  object Button6: TButton

    Left = 79

    Top = 191

    Width = 121

    Height = 25

    Caption = 'Button6'

    TabOrder = 7

    OnClick = Button6Click

  end

  object Button7: TButton

    Left = 201

    Top = 38

    Width = 48

    Height = 178

    Caption = 'Button7'

    TabOrder = 8

    OnClick = Button7Click

  end

end


 
   

你可能感兴趣的:(String)