Pascal - 字串



Pascal 中的字串實際上是由一系列字元組成的序列,可以選擇指定大小。這些字元可以是數字、字母、空格、特殊字元或所有字元的組合。擴充套件 Pascal 根據系統和實現提供了多種型別的字串物件。我們將討論程式中使用的更常見的字串型別。

您可以透過多種方式定義字串:

  • 字元陣列 - 這是一種字元字串,它是由單引號括起來的零個或多個位元組大小的字元組成的序列。

  • 字串變數 - 如 Turbo Pascal 中定義的 String 型別的變數。

  • 短字串 - 帶有大小指定的 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 提供的各種字串操作子程式:

序號 函式及用途
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:):;

將字串轉換為全大寫

15

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

連線兩個字串

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;

比較兩個字串(不區分大小寫)

19 procedure 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;

複製 Pascal 字串的 N 個位元組

35

function UpperCase(const s:):;

將字串轉換為全大寫

廣告