1. 程式人生 > >Delphi中String與WideString的區別

Delphi中String與WideString的區別

AnsiString delphi預設字串型別
WideString 功能上類似於AnsiString,但它是由WideChar字元組成的,也就是。 

Str1:string;// 編譯器認為S的型別是AnsiString 
當然,能用編譯開關$ H來將AnsiString 型別定義為ShortString,當$ H編譯開關的值為負時,S變數 
是ShortString型別;當$ H編譯開關的值為正時(預設情況),字串變數是AnsiString 型別。

另:
AnsiChar:標準的1位元組ANSI字元; 
WideChar:2位元組的Unicode字元; 
Char:等同於AnsiChar;
//測試程式碼
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
  s1:WideString;
  s2:String;
begin
  s1:='JSJ';
  s2:='JSJ';
  showmessage('WideString:'+inttostr(length(s1)));  //長度:3
  showmessage('String:'+inttostr(length(s2)));      //長度:3
  s1:='計算機';
  s2:='計算機';
  showmessage('WideString:'+inttostr(length(s1)));  //長度:3
  showmessage('String:'+inttostr(length(s2)));      //長度:6
end;
end.