Pascal-strings

提供:Dev Guides
移動先:案内検索

パスカル-文字列

Pascalの文字列は、実際にはオプションのサイズ指定のある文字列です。 文字は、数字、文字、空白、特殊文字、またはすべての組み合わせです。 Extended Pascalは、システムと実装に応じて、さまざまなタイプの文字列オブジェクトを提供します。 プログラムで使用されるより一般的なタイプの文字列について説明します。

あなたは多くの方法で文字列を定義することができます-

  • 文字配列-これは、一重引用符で囲まれたゼロ以上のバイトサイズの文字のシーケンスである文字列です。
  • 文字列変数-Turbo Pascalで定義されている文字列型の変数。
  • 短い文字列-サイズが指定された文字列型の変数。
  • ヌル文字列- pchar 型の変数。
  • AnsiStrings -Ansistringsは長さ制限のない文字列です。

Pascalは、1つの文字列演算子、文字列連結演算子(+)のみを提供します。

次のプログラムは、最初の4種類の文字列を出力します。 次の例では、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は、文字列を操作するさまざまな関数とプロシージャをサポートしています。 これらのサブプログラムは実装ごとに異なります。 ここでは、Free Pascalが提供するさまざまな文字列操作サブプログラムをリストしています-

Sr.No. Function & Purpose
1

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

2つの文字列を比較します

2

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

2つの文字列を比較します。大文字と小文字は区別されません

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:);

2つの文字列を追加します

16

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

ヒープ上の文字列の値を割り当てます

17

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

2つの文字列を大文字と小文字を区別して比較します

18

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

大文字と小文字を区別しない2つの文字列を比較します

19

procedure DisposeStr(S: PString); overload;

ヒープから文字列を削除します

20

procedure DisposeStr(S: PShortString); overload;

ヒープから文字列を削除します

21

function IsValidIdent( const Ident:):Boolean;

文字列は有効なパスカル識別子です

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をパスカル文字列に変換します

33

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

パスカル文字列をコピーします

34

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

Nバイトのパスカル文字列をコピーします

35

function UpperCase(const s:):;

文字列をすべて大文字に変換します