Originale-mail to me for new edition

 

Character strings

 

A character string, also called a string literal or string constant, consists of a quoted string, a control string, or a combination of quoted and control strings. Separators can occur only within quoted strings.

A quoted string is a sequence of up to 255 characters from the extended ASCII character set, written on one line and enclosed by apostrophes. A quoted string with nothing between the apostrophes is a null string. Two sequential apostrophes in a quoted string denote a single character, namely an apostrophe. For example,

'BORLAND'        { BORLAND }

'You''ll see'    { You'll see }

''''             { ' }

''               { null string }

' '              { a space }

A control string is a sequence of one or more control characters, each of which consists of the # symbol followed by an unsigned integer constant from 0 to 255 (decimal or hexadecimal) and denotes the corresponding ASCII character. The control string

#89#111#117

is equivalent to the quoted string

'You'

You can combine quoted strings with control strings to form larger character strings. For example, you could use

'Line 1'#13#10'Line 2'

to put a carriage-return-line-feed between Line 1 and Line 2. However, you cannot concatenate two quoted strings in this way, since a pair of sequential apostrophes is interpreted as a single character. (To concatenate quoted strings, use the + operator or simply combine them into a single quoted string.)

A character string’s length is the number of characters in the string. A character string of any length is compatible with any string type and with the PChar type. A character string of length 1 is compatible with any character type, and, when extended syntax is enabled ({$X+}), a nonempty character string of length n is compatible with zero-based arrays and packed arrays of n characters. For more information about string types, see String types.

 

Topic groups

 

See also

Directives

Fundamental syntactic elements: Overview

Identifiers

Labels

Numerals

Reserved words

Special symbols

 

 

译文

 

字符串

 

字符串也叫做文本串或串常量,它由引用串、控制串组或二者联合组成。分隔符只能出现在引用串中。

在扩展ASCII字符集中,用前后两个单引号括起来的单个引用串是一个最长可达255个字符的序列。单引号之间没有字符的引用串是空串(null string)。引用串中连续的两个单引号表示一个字符,即单引号。例如:

'BORLAND'        { BORLAND }

'You''ll see'    { You'll see }

''''             { ' }

''               { 空串 }

' '              { 空格 }

控制串是有一个或更多控制字符的序列,每个控制字符都由 # 号及紧随其后的无符号整型常量(0..255之间,10进制或16进制)组成并且表示响应的ASCII字符。控制串

#89#111#117

等价于引用串

'You'

开可以联合引用串与控制串以构成大的字符串。例如,字符串

'Line 1'#13#10'Line 2'

Line 1Line2之间放置了回车换行。然而,不能通过这种方法连接两个引用串(例如不能试图将引用串 ‘Hello’ 与引用串 ‘World’连接在一起写成 ‘Hello’’World’,该字符串表示的是“Hello’World”,而不是预期结果),因为连续的两个引号将被解释成单个字符。(要连接两个引用串,可以使用串运算符中的加号 + 运算符或简单地将其合并到一对引号中,如’Hello’ + ‘World’,或者直接写成’HelloWorld’。)

字符串的长度是指串中字符的数量。任意长度的字符串与任意串类型(包括PChar类型)兼容。长度为1的字符串与任意字符类型兼容。此外,当扩充语法有效(编译指示{$X+})时,长度为n的非空字符串与零基字符数组、容量为n的压缩字符数组兼容。有关串类型的更多信息,见串类型

 

主题组

 

相关主题

指示字

基本语法元素:概述

标识符

标号

数字

保留字

特殊符号

 

 

编者注

零基数组形如 array [0..m] of ...;零基字符数组形如 array [0..m] of Char 。其中的m是非负的整数。压缩数组形如 packed array [a..b] of ... 。下面的代码就字符数组与字符串兼容性进行了典型说明:

 

var

  PA: packed array [2..5] of Char;   //压缩字符数组

  A: array [0..5] of Char;           //零基字符数组

  B: array [2..5] of Char;           //一般字符数组

  S: string;                         //最常用的串

 

procedure Test;

begin

  PA := 'OK ?';      //类型兼容

  A  := 'Hello!';    //类型兼容

  B  := 'Bye.';      //类型兼容

  ShowMessage(PA);

  ShowMessage(A);

  ShowMessage(B);

  S := PA;       //类型兼容

  ShowMessage(S);

  S := A;        //类型兼容

  ShowMessage(S);

  S := B;        //类型兼容

  ShowMessage(S);

//  PA := S;     //类型不兼容

//  A  := S;     //类型不兼容

//  B  := S;     //类型不兼容

end;

 

通过本例还可以看出,字符串常量和字符串变量对字符数组的兼容性是有区别的(作为传递给过程ShowMessage的参数,定义的四个标识符兼容,而PAAB却都不能向S赋值)。