首先抄錄一下來自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個字節。