📜  Pascal-字符串

📅  最后修改于: 2020-11-03 16:16:23             🧑  作者: Mango


Pascal中的字符串实际上是具有可选大小规格的字符序列。字符可以是数字,字母,空格,特殊字符或全部的组合。扩展Pascal提供了多种类型的字符串对象,具体取决于系统和实现。我们将讨论程序中使用的更常见的字符串类型。

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

  • 字符阵列-这是一个,其是在单引号零个或多个字节大小的字符的序列。

  • 字符串变量-字符串类型的变量,如Turbo Pascal中所定义。

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

  • 空终止的字符串-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. Function & Purpose
1

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

Compares two strings

2

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

Compares two strings, case insensitive

3

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

Removes quotes from string

4

function AnsiLastChar(const S:):PChar;

Gets last character of string

5

function AnsiLowerCase(const s:):

Converts string to all-lowercase

6

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

Quotes a string

7

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

Compares strings case-sensitive

8

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

Compares strings case-insensitive

9

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

Compares L characters of strings case sensitive

10

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

Compares L characters of strings case insensitive

11

function AnsiStrLastChar(Str: PChar):PChar;

Gets last character of string

12

function AnsiStrLower(Str: PChar):PChar;

Converts string to all-lowercase

13

function AnsiStrUpper(Str: PChar):PChar;

Converts string to all-uppercase

14

function AnsiUpperCase(const s:):;

Converts string to all-uppercase

15

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

Appends 2 strings

16

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

Assigns value of strings on heap

17

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

Compares two strings case sensitive

18

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

Compares two strings case insensitive

19 procedure DisposeStr(S: PString); overload;

Removes string from heap

20

procedure DisposeStr(S: PShortString); overload;

Removes string from heap

21

function IsValidIdent( const Ident:):Boolean;

Is string a valid pascal identifier

22

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

Last occurrence of character in a string

23

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

Gets first N characters of a string

24

function LoadStr(Ident: Integer):;

Loads string from resources

25

function LowerCase(const s: ):; overload;

Converts string to all-lowercase

26

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

Converts string to all-lowercase

27

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

Allocates new string on heap

28

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

Gets last N characters of a string

29

function StrAlloc(Size: Cardinal):PChar;

Allocates memory for string

30

function StrBufSize(Str: PChar):SizeUInt;

Reserves memory for a string

31

procedure StrDispose(Str: PChar);

Removes string from heap

32

function StrPas(Str: PChar):;

Converts PChar to pascal string

33

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

Copies pascal string

34

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

Copies N bytes of pascal string

35

function UpperCase(const s:):;

Converts string to all-uppercase