如何把字符串覆给数组 - 回复"刘那"

问题来源: http://www.cnblogs.com/del/archive/2008/05/11/1089344.html#1191871

unit Unit1;



interface



uses

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

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



{把字符串给静态(字符)数组}

procedure TForm1.Button1Click(Sender: TObject);

const

  str = '万一的 Delphi 博客';

var

  StaticArr: array[0..255] of Char;

begin

  {直接赋值即可}

  StaticArr := str;



  {显示}

  ShowMessage(StaticArr); {万一的 Delphi 博客}

end;





{把字符串给动态(字符)数组}

procedure TForm1.Button2Click(Sender: TObject);

const

  str = '万一的 Delphi 博客';

var

  DynamicArr: array of Char;

  i: Integer;

begin

  {设置动态数组大小}

  SetLength(DynamicArr, Length(str));



  {把字符串赋给动态数组}

  for i := 0 to Length(DynamicArr) - 1 do

    DynamicArr[i] := str[i+1];



  {显示动态数组中的字符}

  ShowMessage(PChar(@DynamicArr[0])); {万一的 Delphi 博客}

end;





{数组元素可不一定都是字符}

procedure TForm1.Button3Click(Sender: TObject);

const

  str = '万一的 Delphi 博客';

var

  DynamicArr: array of string;

begin

  SetLength(DynamicArr, 1);

  DynamicArr[0] := str;

  ShowMessage(DynamicArr[0]); {万一的 Delphi 博客}

end;



end.


 
   

你可能感兴趣的:(字符串)