Pascal - Strings

Pascal中的字符串实际上是一个具有可选大小规范的字符序列.字符可以是数字,字母,空白,特殊字符或所有字符的组合. Extended Pascal根据系统和实现提供多种类型的字符串对象.我们将讨论程序中使用的更常见的字符串类型.

您可以通过多种方式定义字符串 :

  • 字符数组 : 这是一个字符串,它是用单引号括起来的零个或多个字节大小的字符序列.

  • 字符串变量 :  ; String类型的变量,如Turbo Pascal中所定义.

  • 短字符串 : 具有大小规格的String类型的变量.

  • Null终止字符串 : 变量 pchar 类型.

  • AnsiStrings :  Ansistrings是没有长度限制的字符串.

Pascal只提供一个字符串运算符,字符串连接运算符(+).

示例

以下程序打印前四种字符串.我们将在下一个例子中使用AnsiStrings.

program exString;
var
   greetings: string;
   name: packed array [1..10] of char;
   organisation: string[10];
   message: pchar;

begin
   greetings := 'Hello ';
   message := 'Good Day!';
   
   writeln('Please Enter your Name');
   readln(name);
   
   writeln('Please Enter the name of your Organisation');
   readln(organisation);
   
   writeln(greetings, name, ' from ', organisation);
   writeln(message); 
end.

编译并执行上述代码时,会产生以下结果 :

Please Enter your Name
John Smith
Please Enter the name of your Organisation
Infotech
Hello John Smith from Infotech

以下示例使用了更多函数,让我们看看 : 

program exString;
uses sysutils;
var
   str1, str2, str3 : ansistring;
   str4: string;
   len: integer;

begin
   str1 := 'Hello ';
   str2 := 'There!';
   
   (* copy str1 into str3 *)
   str3 := str1;
   writeln('appendstr( str3, str1) :  ', str3 );
   
   (* concatenates str1 and str2 *)
   appendstr( str1, str2);
   writeln( 'appendstr( str1, str2) ' , str1 );
   str4 := str1 + str2;
   writeln('Now str4 is: ', str4);
   
   (* total lenghth of str4 after concatenation  *)
   len := byte(str4[0]);
   writeln('Length of the final string str4: ', len); 
end.

编译并执行上述代码时,会产生以下结果 :

appendstr( str3, str1) : Hello
appendstr( str1, str2) : Hello There!
Now str4 is: Hello There! There!
Length of the final string str4: 18

Pascal字符串函数和过程

Pascal支持广泛操作字符串的函数和过程的范围.这些子程序在实施方面有所不同.这里,我们列出了Free Pascal : 提供的各种字符串操作子程序;

Sr.No.功能&目的
1

function AnsiCompareStr(const S1 :; const S2 :):Integer;

比较两个字符串

2

function AnsiCompareText(const S1: ; const S2:):Integer;

比较两个字符串,不区分大小写

3

function AnsiExtractQuotedStr(var Src: PChar; Quote: Char):;

从字符串中移除引号

4

function AnsiLastChar(const S:):PChar;

获取字符串

5

function AnsiLowerCase(const s:):

将字符串转换为全小写

6

function AnsiQuotedStr(const S: ; Quote: Char):;

引用字符串

7

function AnsiStrComp(S1: PChar;S2: PChar):Integer;

比较字符串区分大小写

8

function AnsiStrIComp(S1: PChar; S2: PChar):Integer;

比较不区分大小写的字符串

9

function AnsiStrLComp(S1: PChar; S2: PChar; MaxLen: Cardinal):Integer;

比较区分大小写的L个字符区分大小写

10

function AnsiStrLIComp(S1: PChar; S2: PChar; MaxLen: Cardinal):Integer;

比较字符串的L个字符不区分大小写

11

function AnsiStrLastChar(Str:PChar):PChar;

获取字符串的最后一个字符

12

function AnsiStrLower(Str:PChar):PChar;

将字符串转换为全小写

13

function AnsiStrUpper(Str:PChar):PChar;

将字符串转换为全大写

14

function AnsiUpperCase(const s:):;

转换字符串to all-uppercase

15

procedure AppendStr(var Dest: ; const S:);

追加2个字符串

16

procedure AssignStr(var P: PString; const S:);

在堆上分配字符串的值

17

function CompareStr(const S1: ; const S2:):Integer; overload;

比较两个字符串区分大小写

18

function CompareText(const S1: ; const S2:):Integer;

比较两个不区分大小写的字符串

19procedure DisposeStr(S: PString); overload;

从堆中删除字符串

20

procedure DisposeStr(S: PShortString); overload;

从堆中删除字符串

21

function IsValidIdent(const Ident :):Boolean;

字符串是有效的pascal标识符

22

function LastDelimiter(const Delimiters: ; const S:):Integer;

字符串中最后出现的字符

23

function LeftStr(const S: ; Count: Integer):;

获取字符串的前N个字符

24

function LoadStr(Ident: Integer):;

从资源中加载字符串

25

function LowerCase(const s: ):; overload;

将字符串转换为全小写

26

function LowerCase(const V: variant ):; overload;

将字符串转换为全小写

27

function NewStr(const S:):PString; overload;

在堆上分配新字符串

28

function RightStr(const S: ; Count: Integer):;

获取字符串的最后N个字符

29

function StrAlloc(Size: Cardinal):PChar;

为字符串分配内存

30

function StrBufSize(Str: PChar):SizeUInt;

为字符串保留内存

31

procedure StrDispose(Str: PChar);

从堆中删除字符串

32

function StrPas(Str: PChar):;

将PChar转换为pascal字符串

33

function StrPCopy(Dest: PChar; Source:):PChar;

复制pascal字符串

34

function StrPLCopy(Dest: PChar; Source: ; MaxLen: SizeUInt):PChar;

复制N个字节的pascal字符串

35

function UpperCase(const s:):;

将字符串转换为全部-uppercase