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.

你可能感兴趣的:(D7)