Delphi character and string [4]

Source: Internet
Author: User

string and character arraysvar
arr: array[0..5] of Char;
str: string;
begin
{可以把字符串常量直接赋给字符数组; 但超界不行}
arr := 'Delphi';
ShowMessage(arr); {Delphi}
{可以把字符数组直接赋给字符串变量}
str := arr;
ShowMessage(str); {Delphi}
{但不能把一个字符串变量赋给字符数组}
//arr := str;    {错误}
{其实字符串内部也是包含了一个字符数组, 所以能索引访问, 不过它的索引起始于 1}
ShowMessage(str[1]); {D}
ShowMessage(arr[0]); {D}
end;

character Pointers and character arrays

var
arr: array[0..6] of Char;
p: PChar;
begin
arr := 'Delphi';
{如果直接把字符数组给字符指针, 结果不保险, 因为字符指针要找空字符(#0)结束}
{把数组的最后一个元素给 #0 就可以了}
arr[Length(arr)-1] := #0;
p := arr;
ShowMessage(p); {Delphi}
{假如把 #0 给到中间会怎样?}
arr[3] := #0;
p := arr;
ShowMessage(p); {Del; 给截断了}
end;

String constants > number of characters


Const
ARR1:ARRAY[0..5] of Char = ' Delphi ';
ARR2:ARRAY[0..5] of Ansichar = ansistring (' Delphi ');
Begin
ShowMessage (Arr1[0]); D
ShowMessage (arr2[0]); D
End

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.