您的位置:首页 > 其它

学习string类型的一点记录

2010-03-28 00:40 309 查看
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的长度的,不能放其它字符。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: