Delphi 2010 char widechar ansichar string ansistring widestring shortstring的測試

本文详细介绍了Delphi中各种字符类型的定义与使用方法,包括AnsiChar、WideChar、AnsiString、WideString等,并通过实例展示了不同字符类型在处理中文字符时的表现差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先抄錄一下來自Delphi Basics的說明:

 Type         AnsiChar             A character type guaranteed to be 8 bits in size  (8位)
 Type         AnsiString           A data type that holds a string of AnsiChars
 Type         Char                   Variable type holding a single character
 Type         ShortString         Defines a string of up to 255 characters                (255字節)
 Type         String                 A data type that holds a string of characters
 Type         WideChar           Variable type holding a single International character
 Type         WideString         A data type that holds a string of WideChars

 

代碼如下:

var c:Char;
    ac:ansichar;
    wc:widechar;
    ans:ansistring;
    ws:widestring;
    ss:shortstring;
    s:string;
begin
 c:='國';
 memo2.Lines.Add(c+'   Char   '
                  +'  Length:  '+inttostr(length(c))
                  +'  Sizeof:  '+inttostr(Sizeof(c)));
 c:='国';
 memo2.Lines.Add(c+'   Char   '
                  +'  Length:  '+inttostr(length(c))
                  +'  Sizeof:  '+inttostr(Sizeof(c)));

 ac:='國';
 memo2.Lines.Add(ac+'   AnsiChar   '
                  +'  Length:  '+inttostr(length(ac))
                  +'  Sizeof:  '+inttostr(Sizeof(ac)));
 ac:='国';
 memo2.Lines.Add(ac+'   AnsiChar   '
                  +'  Length:  '+inttostr(length(ac))
                  +'  Sizeof:  '+inttostr(Sizeof(ac)));

 wc:='國';
 memo2.Lines.Add(wc+'   WideChar   '
                  +'  Length:  '+inttostr(length(wc))
                  +'  Sizeof:  '+inttostr(Sizeof(wc)));
 wc:='国';
 memo2.Lines.Add(wc+'   WideChar   '
                  +'  Length:  '+inttostr(length(wc))
                  +'  Sizeof:  '+inttostr(Sizeof(wc)));

 ans:='國';
 memo2.Lines.Add(ans+'   AnsiString   '
                  +'  Length:  '+inttostr(length(ans))
                  +'  Sizeof:  '+inttostr(Sizeof(ans)));
 ans:='国';
 memo2.Lines.Add(ans+'   AnsiString   '
                  +'  Length:  '+inttostr(length(ans))
                  +'  Sizeof:  '+inttostr(Sizeof(ans)));

 ws:='國';
 memo2.Lines.Add(ws+'   WideString   '
                  +'  Length:  '+inttostr(length(ws))
                  +'  Sizeof:  '+inttostr(Sizeof(ws)));
 ws:='国';
 memo2.Lines.Add(ws+'   WideString   '
                  +'  Length:  '+inttostr(length(ws))
                  +'  Sizeof:  '+inttostr(Sizeof(ws)));

 ss:='國';
 memo2.Lines.Add(ss+'   ShortString   '
                  +'  Length:  '+inttostr(length(ss))
                  +'  Sizeof:  '+inttostr(Sizeof(ss)));
 ss:='国';
 memo2.Lines.Add(ss+'   ShortString   '
                  +'  Length:  '+inttostr(length(ss))
                  +'  Sizeof:  '+inttostr(Sizeof(ss)));

 s:='國';
 memo2.Lines.Add(s+'   String   '
                  +'  Length:  '+inttostr(length(s))
                  +'  Sizeof:  '+inttostr(Sizeof(s)));
 s:='国';
 memo2.Lines.Add(s+'   String   '
                  +'  Length:  '+inttostr(length(s))
                  +'  Sizeof:  '+inttostr(Sizeof(s)));

 

 

結果如下:

 

國   Char     Length:  1  Sizeof:  2
国   Char     Length:  1  Sizeof:  2


?   AnsiChar     Length:  1  Sizeof:  1

                                                         --沒有顯示出來,如果是ansic碼的話OK,也就是說只能表示ansic碼
國   WideChar     Length:  1  Sizeof:  2
国   WideChar     Length:  1  Sizeof:  2


國   AnsiString     Length:  2  Sizeof:  4
?   AnsiString     Length:  1  Sizeof:  4


國   WideString     Length:  1  Sizeof:  4
国   WideString     Length:  1  Sizeof:  4


國   ShortString     Length:  2  Sizeof:  256
?   ShortString     Length:  1  Sizeof:  256


國   String     Length:  1  Sizeof:  4
国   String     Length:  1  Sizeof:  4

 

說明delphi支援Unicode之後,字符串型(string )佔用4個字節,char佔用2個字節。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值