Originale-mail to me for new edition

 

String types

 

A string represents a sequence of characters. Object Pascal supports the following predefined string types.

 

Type

Maximum length

Memory required

Used for

ShortString

255 characters

2 to 256 bytes

backward compatibility

AnsiString

~2^31 characters

4 bytes to 2GB

8-bit (ANSI) characters

WideString

~2^30 characters

4 bytes to 2GB

multiuser servers and multi-language applications

 

AnsiString, sometimes called the long string, is the preferred type for most purposes.

String types can be mixed in assignments and expressions; the compiler automatically performs required conversions. But strings passed by reference to a function or procedure (as var and out parameters) must be of the appropriate type. Strings can be explicitly cast to a different string type (see Typecasts).

The reserved word string functions like a generic type identifier. For example,

var S: string;

creates a variable S that holds a string. In the default {$H+} state, the compiler interprets string (when it appears without a bracketed number after it) as AnsiString. Use the {$H-} directive to turn string into ShortString.

The standard function Length returns the number of characters in a string. The SetLength procedure adjusts the length of a string.

Comparison of strings is defined by the ordering of the characters in corresponding positions. Between strings of unequal length, each character in the longer string without a corresponding character in the shorter string takes on a greater-than value. For example, ‘AB’ is greater than ‘A’, that is, 'AB' > 'A' returns True. Zero-length strings hold the lowest values.

You can index a string variable just as you would an array. If S is a string variable and i an integer expression, S[i] represents the ith character or, strictly speaking, the ith byte in S. For a ShortString or AnsiString, S[i] is of type AnsiChar; for a WideString, S[i] is of type WideChar. The statement MyString[2] := 'A'; assigns the value A to the second character of MyString. The following code uses the standard UpCase function to convert MyString to uppercase.

var I: Integer;

begin

  I := Length(MyString);

  while I > 0 do

  begin

    MyString[I] := UpCase(MyString[I]);

    I := I - 1;

  end;

end;

Be careful indexing strings in this way, since overwriting the end of a string can cause access violations. Also, avoid passing long-string indexes as var parameters, because this results in inefficient code.

You can assign the value of a string constant or any other expression that returns a string to a variable. The length of the string changes dynamically when the assignment is made. Examples:

MyString := 'Hello world!';

MyString := 'Hello ' + 'world';

MyString := MyString + '!';

MyString := ' ';             { space }

MyString := '';              { empty string }

 

Topic groups

 

See also

About types

Character strings

Data types and variables: Overview

Long strings

Short strings

String operators

WideString

Working with null-terminated strings

 

 

译文

 

串类型

 

串表示字符序列。Object Pascal提供下列预定义串类型:

 

类型

最大长度

占用内存

用于

ShortString

255个字符

2256字节

向后兼容

AnsiString

大约2^31个字符

4字节到2GB

8位(ANSI)字符

WideString

大约2^30个字符

4字节到2GB

多用户服务和多语言应用程序

 

AnsiString有时也叫做long stirng,大多数情况下首选该类型。

各种串类型可以在赋值语句和表达式中混合适用,编译器自动执行必需的转换。然而,以参数形式传递给函数或过程的串(作为var参数或out参数)必需具有适当的类型。串可以之间转换为不同的串类型(见类型转换)。

保留字string的作用有如一般类型的标识符。例如,

var S: string;

创建了变量S,用于保存串。在默认的编译指示{$H+}状态下,编译器将string(后面没有跟随方括号和数字)解释为AnsiString;使用编译指示{$H-}时,解释为ShortString

标准函数Length返回串中字符的个数;标准过程SetLength可以调整串的长度。

串的比较由定义为串中相应位置字符顺序比较。在不等长的两个串中,对于较长的串中的每个字符,如果在较短的串中没有相应位置可比较的字符,那么将视为“大于”。例如,串‘AB’大于串‘A’,也就是说,表达式 'AB' > 'A' 返回True。零长度串(空串)保存最小串值。

可以象索引数组那样对串进行索引。如果S是一个串变量,i是一个整数表达式,那么S[i]表示串S中第 i 个字符或者直接地说,是S中的第i个字节。对ShortStringAnsiStringS[i]AnsiChar类型;对WideStringS[i]WideChar类型。语句 MyString[2] := ‘A’; 把值 A 赋给MyString的第二个字符。下面的代码用标准函数UpCaseMyString转换为大写。

var I: Integer;

begin

  I := Length(MyString);

  while I > 0 do

  begin

    MyString[I] := UpCase(MyString[I]);

    I := I - 1;

  end;

end;

通过这种方法对串进行索引时要特别小心,因为重写串尾可能导致访问非法。同样,应避免将长串索引作为var参数传递,因为此举将使代码效率降低。

可以将串常量的值和其他任何返回串的表达式赋给串变量。赋值语句完成时,串的长度会动态改变。下面是对串变量赋值的范例:

MyString := 'Hello world!';

MyString := 'Hello ' + 'world';

MyString := MyString + '!';

MyString := ' ';             { 空格 }

MyString := '';              { 空串 }

 

主题组

 

相关主题

关于类型

字符串

数据类型和变量:概述

长串

短串

串运算符

宽串

空结束串

 

 

编者注

 

关于串的比较,原文和译文似乎都解释得不够清晰。可以用下列规则进行更为准确的描述:

对于串S1S2S0

1、   如果S1S2均为空串,那么S1 = S2返回True

2、   如果S1不是空串而S2是空串,那么S1 > S2返回True

3、   如果S1S2均不是空串且长度相等为n,那么从第1到第n对字符 S1[x] S2[x] x即为1n顺序比较)进行比较,如果出现不相等的字符对,那么以首次出现时字符对比较结果作为串的比较结果;如果所有的字符对全部相等,那么S1 = S2 返回True

4、   如果S1的长度大于S2,长度依次为ab,并且S1S2均不是空串,假定S3是长度为b的串,并且第1到第b个字符与S1中第1到第b个字符对应相同;那么,如果S3S2不等,那么其比较结果作为S1S2的比较结果,否则S1 > S2返回True

5、   如果S1 = S2返回True,那么S1 >= S2S1 <= S2返回True;如果S1 > S2返回True,那么S1 >= S2返回True;如果S1 < S2返回True,那么S1 <= S2返回True

注意:串的第一个字符其索引为1,即 S[1] 表示串S的第一个字符。

根据以上规则,下列表达式均返回True

'' = ''

'B' > 'A'

'AX' < 'BC'

'ABABCCC' > 'ABAB'

'ABABCCC' < 'B'

'GHJK' > ''

'ABCD' >= 'ABAB'