Pascal - Strings
A string em Pascal é na verdade uma sequência de caracteres com uma especificação de tamanho opcional. Os caracteres podem ser numéricos, letras, espaços em branco, caracteres especiais ou uma combinação de todos. O Extended Pascal fornece vários tipos de objetos de string, dependendo do sistema e da implementação. Discutiremos os tipos mais comuns de strings usados em programas.
Você pode definir uma string de várias maneiras -
Character arrays - Esta é uma sequência de caracteres que é uma sequência de zero ou mais caracteres de tamanho de byte entre aspas simples.
String variables - A variável do tipo String, conforme definido no Turbo Pascal.
Short strings - A variável do tipo String com especificação de tamanho.
Null terminated strings - A variável de pchar tipo.
AnsiStrings - Ansistrings são strings sem limite de comprimento.
Pascal fornece apenas um operador de string, operador de concatenação de string (+).
Exemplos
O programa a seguir imprime os primeiros quatro tipos de strings. Usaremos AnsiStrings no próximo exemplo.
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.
Quando o código acima é compilado e executado, ele produz o seguinte resultado -
Please Enter your Name
John Smith
Please Enter the name of your Organisation
Infotech
Hello John Smith from Infotech
O exemplo a seguir usa mais algumas funções, vamos ver -
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.
Quando o código acima é compilado e executado, ele produz o seguinte resultado -
appendstr( str3, str1) : Hello
appendstr( str1, str2) : Hello There!
Now str4 is: Hello There! There!
Length of the final string str4: 18
Funções e procedimentos de string Pascal
Pascal suporta uma ampla gama de funções e procedimentos que manipulam strings. Esses subprogramas variam em termos de implementação. Aqui, estamos listando vários subprogramas de manipulação de strings fornecidos pelo Free Pascal -
Sr. Não. | Função e objetivo |
---|---|
1 | function AnsiCompareStr(const S1: ; const S2:):Integer; Compara duas strings |
2 | function AnsiCompareText(const S1: ; const S2:):Integer; Compara duas strings, não diferencia maiúsculas de minúsculas |
3 | function AnsiExtractQuotedStr(var Src: PChar; Quote: Char):; Remove as aspas da string |
4 | function AnsiLastChar(const S:):PChar; Obtém o último caractere da string |
5 | function AnsiLowerCase(const s:): Converte string em minúsculas |
6 | function AnsiQuotedStr(const S: ; Quote: Char):; Cita uma string |
7 | function AnsiStrComp(S1: PChar;S2: PChar):Integer; Compara strings com distinção entre maiúsculas e minúsculas |
8 | function AnsiStrIComp(S1: PChar; S2: PChar):Integer; Compara strings que não diferenciam maiúsculas de minúsculas |
9 | function AnsiStrLComp(S1: PChar; S2: PChar; MaxLen: Cardinal):Integer; Compara caracteres L de strings com distinção entre maiúsculas e minúsculas |
10 | function AnsiStrLIComp(S1: PChar; S2: PChar; MaxLen: Cardinal):Integer; Compara caracteres L de strings que não diferenciam maiúsculas de minúsculas |
11 | function AnsiStrLastChar(Str: PChar):PChar; Obtém o último caractere da string |
12 | function AnsiStrLower(Str: PChar):PChar; Converte string em minúsculas |
13 | function AnsiStrUpper(Str: PChar):PChar; Converte string em maiúsculas |
14 | function AnsiUpperCase(const s:):; Converte string em maiúsculas |
15 | procedure AppendStr(var Dest: ; const S:); Acrescenta 2 strings |
16 | procedure AssignStr(var P: PString; const S:); Atribui valor de strings no heap |
17 | function CompareStr(const S1: ; const S2:):Integer; overload; Compara duas strings com distinção entre maiúsculas e minúsculas |
18 | function CompareText(const S1: ; const S2:):Integer; Compara duas strings que não diferenciam maiúsculas de minúsculas |
19 | procedure DisposeStr(S: PString); overload; Remove string do heap |
20 | procedure DisposeStr(S: PShortString); overload; Remove string do heap |
21 | function IsValidIdent( const Ident:):Boolean; A string é um identificador pascal válido |
22 | function LastDelimiter(const Delimiters: ; const S:):Integer; Última ocorrência de caractere em uma string |
23 | function LeftStr(const S: ; Count: Integer):; Obtém os primeiros N caracteres de uma string |
24 | function LoadStr(Ident: Integer):; Carrega string de recursos |
25 | function LowerCase(const s: ):; overload; Converte string em minúsculas |
26 | function LowerCase(const V: variant ):; overload; Converte string em minúsculas |
27 | function NewStr(const S:):PString; overload; Aloca nova string na pilha |
28 | function RightStr(const S: ; Count: Integer):; Obtém os últimos N caracteres de uma string |
29 | function StrAlloc(Size: Cardinal):PChar; Aloca memória para string |
30 | function StrBufSize(Str: PChar):SizeUInt; Reserva memória para uma string |
31 | procedure StrDispose(Str: PChar); Remove string do heap |
32 | function StrPas(Str: PChar):; Converte PChar em string pascal |
33 | function StrPCopy(Dest: PChar; Source:):PChar; Copia string pascal |
34 | function StrPLCopy(Dest: PChar; Source: ; MaxLen: SizeUInt):PChar; Copia N bytes de string pascal |
35 | function UpperCase(const s:):; Converte string em maiúsculas |