Pascal - 字符串
Pascal 中的字符串实际上是具有可选大小规范的字符序列。 字符可以是数字、字母、空格、特殊字符或所有字符的组合。 Extended Pascal 根据系统和实现提供多种类型的字符串对象。 我们将讨论程序中使用的更常见的字符串类型。
可以通过多种方式定义字符串 −
字符数组 − 这是一个字符串,是用单引号括起来的零个或多个字节大小的字符的序列。
字符串变量 − String类型的变量,在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 提供的各种字符串操作子程序 −
| 序号 | 功能和目的 |
|---|---|
| 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:); 附加 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; 比较两个字符串,不区分大小写 |
| 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; 复制 N 个字节的 pascal 字符串 |
| 35 |
function UpperCase(const s:):; 将字符串转换为全大写 |

