学习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的长度的,不能放其它字符。
看看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的长度的,不能放其它字符。
相关文章推荐
- 黑马程序员_学习记录12:String、StringBuffer、基本数据类型对象包装类
- JS中的Array和String,Date类型的一点小操作
- c++primer学习笔记-----3.2标准库类型string
- Fortran学习记录1(Fortran数据类型)
- Java学习笔记06 String和基本数据类型
- Redis学习记录之命令String(八)
- 标准库string类型学习
- Redis学习和应用记录(2)--常用数据类型及命令
- 3/23学习String源码笔记记录
- swift学习笔记之string 类型转换
- Java基础知识学习三 (String类型,字符串处理)
- Java学习之数组1(1.数组的声明;2.元素为引用数据类型的数组;3.关于main方法里的String[] args;4.数组排序;5.数3退1 数组算法,(用数组模拟链表);6数组查找之二分法;7数组的拷贝)
- JavaScript基础学习<二>String类型
- c++ primer学习笔记3-标准库类型string&vector
- C#学习记录 运算符与类型的强制转换
- 学习结构[记录]类型(3) - 结构间的关系
- 关于String类型的一点讲解
- C++学习笔记-----std::string的=,+,+=对int,char类型操作数的支持
- 共同学习Java源码--常用数据类型--String(十六)
- redis学习(二)——String数据类型