- Pascal 教程
- Pascal - 首页
- Pascal - 概述
- Pascal - 环境设置
- Pascal - 程序结构
- Pascal - 基本语法
- Pascal - 数据类型
- Pascal - 变量类型
- Pascal - 常量
- Pascal - 运算符
- Pascal - 决策
- Pascal - 循环
- Pascal - 函数
- Pascal - 过程
- Pascal - 变量作用域
- Pascal - 字符串
- Pascal - 布尔值
- Pascal - 数组
- Pascal - 指针
- Pascal - 记录
- Pascal - 变体
- Pascal - 集合
- Pascal - 文件处理
- Pascal - 内存
- Pascal - 单元
- Pascal - 日期和时间
- Pascal - 对象
- Pascal - 类
- Pascal 有用资源
- Pascal - 快速指南
- Pascal - 有用资源
- Pascal - 讨论
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:):; 将字符串转换为全大写 |