Delphi character and string [2]

Source: Internet
Author: User

Delphi characters and Strings [2]-Char, Ansichar, Widechar, Pchar, Pansichar, Pwidechar

Given Char, Ansichar (in current version (2007), they are one thing, only 1 bytes in size)var
c: Char; {Char 类型的取值范围是: #0..#255, 用十六进制表示是: #$0..#$FF}
begin
{ 用十进制方式赋值:}
c := #65;
ShowMessage(c); {A}
{用十六进制方式赋值:}
c := #$41;
ShowMessage(c); {A}
{用 Chr 函数代替 # 符号}
c := Chr(65);
ShowMessage(c); {A}
c := Chr($41);
ShowMessage(c); {A}
{Char 长度当然会是 1}
ShowMessage(IntToStr(Length(c))); {1}
{Char、AnsiChar 允许这样方便地赋值(也就是 和 1 字节长度的字符串是兼容的):}
c := 'B';
ShowMessage(c); {B}
end;
UniCode character Widechar; Unlike Ansichar, Widechar is 2 bytes in size.var
c: WideChar; {WideChar 的取值范围是: #0..#65535, 用十六进制表示是: #$0..#$FFFF}
begin
{WideChar 兼容了 AnsiChar 的 #0..#255; 但占用了 2 字节大小}
c := #65;
ShowMessage(c); {A}
ShowMessage(IntToStr(Length(c))); {1; 这是字符长度 }
ShowMessage(IntToStr(SizeOf(c))); {2; 但占用 2 个字节}
{用十六进制赋值}
c := #$4E07;
ShowMessage(c); {万}
ShowMessage(IntToStr(Length(c))); {1; 这是字符长度 }
ShowMessage(IntToStr(SizeOf(c))); {2; 但占用 2 个字节}
{用十进制赋值}
c := #19975;
ShowMessage(c); {万}
{如果不超出 #255 的范围是可以直接赋值的}
c := 'B';
ShowMessage(c); {万}
{这样不行}
//c := '万'; {这是 Delphi 的支持问题, 估 计 Delphi 2008 应该可以解决}
{可以这样变通一下:}
c := WideString('万')[1];
ShowMessage(c); {万}
{用 WideChar 的方式显示我的名字}
ShowMessage(#19975#19968);   {万一}
ShowMessage(#19975 + #19968); {万一}
ShowMessage(#$4e07#$4e00);  {万 一}
end;
Character Pointer Pchar, Pansichar; They are not different in the current version (2007).var
p: PChar;
str: string;
begin
{可以给 PChar 直接赋予字符串常量}
p := ' 万一';
ShowMessage(p);          {万一}
ShowMessage(IntToStr(Length(p))); {4}
{给变量值需要转换}
str := '万一的 Delphi 博客';
p := PChar(str); {转换}
ShowMessage(p);          {万一的 Delphi 博客}
ShowMessage(IntToStr (Length(p))); {18}
end;
Wide character Pointer Pwidecharvar
p: PWideChar;
str: WideString; {注意这里不是 String}
begin
{可以给 PWideChar 直接赋予字符串常 量}
p := '万一';
ShowMessage(p);          {万一}
ShowMessage (IntToStr(Length(p))); {2}
{给变量值需要转换}
str := '万一的 Delphi 博客';
p := PWideChar(str); {转换}
ShowMessage(p);          {万一的 Delphi 博客}
ShowMessage(IntToStr(Length(p))); {13}
end;

Alas ~: Code coloring here again problem, but now not in the mood to revise, later say it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.