学习string类型的一点记录

tmp : string[1] 这句代码是什么时候意思?

看看Delphi的帮助:

A ShortString is 0 to 255 characters long. While the length of a ShortString can change dynamically, its memory is a statically allocated 256 bytes; the first byte stores the length of the string, and the remaining 255 bytes are available for characters. If S is a ShortString variable, Ord(S[0]), like Length(S), returns the length of S; assigning a value to S[0], like calling SetLength, changes the length of S. ShortString is maintained for backward compatibility only.

The Delphi language supports short-string types--in effect, subtypes of ShortString--whose maximum length is anywhere from 0 to 255 characters. These are denoted by a bracketed numeral appended to the reserved word string. For example,

var MyString: string[100];

creates a variable called MyString whose maximum length is 100 characters. This is equivalent to the declarations

type CString = string[100];
var MyString: CString;

Variables declared in this way allocate only as much memory as the type requires--that is, the specified maximum length plus one byte. In our example, MyString uses 101 bytes, as compared to 256 bytes for a variable of the predefined ShortString type.

When you assign a value to a short-string variable, the string is truncated if it exceeds the maximum length for the type.

The standard functions High and Low operate on short-string type identifiers and variables. High returns the maximum length of the short-string type, while Low returns zero.

 

注意下面两行:

type CString = string[100];
var MyString: CString;

这两行中定义的MyString有多长呢?按照上面的说法,MyString有101个字节,如果给MyString[0]赋值,或调用SetLength, 就改变了MyString的长度,不过,MyString最长是256个字节,而且只能放255个字符,因为MyString[0]是存放MyString的长度的,不能放其它字符。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值