Pascal. Strings of characters of type String

Strings of characters of type String. Operations on character strings


Contents


1. Representation of character strings in the Pascal language. String type. General information

Pascal defines two types of strings:

  • strings of type String;
  • strings of type PChar. You can read more about PChar strings here.

Character strings are nothing more than an array of characters. This composite data type occupies an important place in the development of various programs in Pascal-oriented languages. Based on this context, standard Pascal allows two ways of storing character arrays in computer memory:

  • unpacked;
  • packed.

The description of the unpacked array of characters looks like this:

var
  M: Array[1..Max] of char;

here

  • M – array name;
  • Max – the maximum possible number of elements in the array.

Description of a packed character array contains an additional word packed

var
  M: Packed Array[1..Max] of char;

To convert a character array from an unpacked form to a packed one and vice versa, the Pascal language has standard functions Pack, UnPack.

Any character string is nothing but a packed array of characters. For convenient description and use of one-dimensional packed character arrays, the String type is introduced in Pascal. Working with the String type is supported by many standard functions of the Pascal language. These functions make it easy to perform routine (basic) operations on character strings.

A character string of type String can be assigned to either a constant or a variable. Any constant or variable can declare a character string in one of two ways:

  • with a specified string length;
  • without specifying the length of the string. In this case, the string length is set to 255 characters. This is the maximum possible length of a string of type String.

If the length of a string is given, then the declaration of a variable type String looks something like this:

var
  s : String[50];

here

  • s – declared string;
  • 50 – the maximum possible string length.

If the string s is declared without specifying the length, then the form of such a declaration is as follows:

var
  s : String;

The same declaration rules apply to constants and types.

In memory, a string of type String occupies 1 byte more than its length. The zero position of the line indicates the length of the line (Figure 1) – the number of characters in the line.

Pascal. Representation of a string of type String in computer memoryFigure 1. Representation of a string of type String in computer memory

The String type with no length specification is compatible with all string types. A single character in a string is of type Char.

 

2. Examples of declaring strings of type String
type
  // Declaring string types
  MySting20 = String[20];
  IdCode = String[10]; // Identification code
  Character = String[1]; // Single character as a string

const
  // Declaring strings as typed constants
  Mon : String = 'Monday';
  Jan : String = 'January';

var
  // Declaring strings as variables
  s1 : String[50];
  s2 : String;

 

3. List of operations, procedures and functions that work with character strings

The following operations are defined for strings of type String:

  • :=assignment;
  • +concatenation;
  • comparison. To compare strings, any relational operators (=, <>, >, <, >=, <=) are used. Strings are compared character by character, starting with the first character. Strings are equal if they have the same length and are character-by-character equivalent.

An index operation is defined to access a single character in a string

S[i]

here

  • S – string;
  • i – character position in the string, which is numbered from 1.

The following procedures and functions are used with character strings:

  • Concat – concatenates two strings;
  • Copy – returns a substring from a string;
  • Delete – deletes a substring from a string;
  • Insert – inserts (adds) a substring to a string;
  • Length – returns the length of a string;
  • Pos – searches for a substring in a string;
  • Val – converts a string to a number;
  • Str – converts a number to a string.

 

4. Access to a single character in the string S[i]. Indexing operation

An indexing operation is used to get a single character of a string. In its simplest form, the general form of the operation of obtaining a character

c := S[index];

here

  • c – character of type Char;
  • s – original string;
  • index – character position in the string. Index numbering starts from 1.

The indexing operation can also be used on the left side of an assignment operator. This means that it is allowed to change the value in the string. An approximate value change code is as follows

S[index] := c;

here

  • s – a string of type String
  • index – position in the string to be changed. The index value is numbered from 1;
  • c – character of type Char, replacing the character S[index].

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  c : Char;
  s : String;

begin
  // Specified string
  S := 'Hello, world!';

  // Get character with index 5
  c := S[5];
  Writeln('c = ', c); // c = o

  // Get character with index 1
  c := S[1];
  Writeln('c = ', c); // c = H

  // Get the last character of a string
  c := S[Length(s)];
  Writeln('c = ', c); // c = !

  // Attempt to change the value of S[8]
  S[8] := 'W'; // допускается
  Writeln(S); // Hello, World!

  Readln;
end.

Program result

c = o
c = H
c = !
Hello, World!

 

5. String assignment operation :=

A useful operator for working with strings is the := operator, which performs string assignments. The general use of the operation is as follows:

S1 := S2;

here

  • S1 – the destination string that receives the value of string S2. The previous value of the string S1 is lost;
  • S2 – source string.

When assigning strings, a full copy of the source string is made. This means that the strings are placed in different areas of memory.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  s1 : String;
  s2 : String;

begin
  // Set value to strings
  S1 := 'Pascal';
  S2 := 'Delphi';

  // Assign the string S1 a new value
  S1 := S2;

  // Display the result
  Writeln('S1 = ', S1);
  Writeln('S2 = ', S2);

  // Change string S2
  S2 := 'JavaScript';

  // Display S1 and S2
  Writeln('S1 = ', S1);
  Writeln('S2 = ', S2);

  Readln;
end.

Program result

S1 = Delphi
S2 = Delphi
S1 = Delphi
S2 = JavaScript

 

6. Operation +. String concatenation

Adding (concatenating) strings can be done using the + (plus) operator. In the most general case, the implementation of string concatenation looks like this:

s := s1 + s2 + ... + sN;

here

  • s – string, which is the result of concatenation of strings s1, s2, …, sN;
  • s1, s2, sN – some strings.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S1, S2, S3 : String;

begin
  // String concatenation
  S1 := 'Hello';
  S2 := 'world';
  S3 := S1 + ', ' + S2 + '!';
  Writeln(S3); // Hello, world!

  S1 := 'ABCD';
  S1 := S1 + S1;
  Writeln(S1); // ABCDABCD

  Readln;
end.

 

7. Operations =, <>. Comparing strings for equality (inequality)

In Pascal, strings can be compared for equality/inequality. To compare strings, the operations =, <> are used according to the following way

S1 = S2
S1 <> S2

The result of the operations is the boolean value True or False. String comparison is used in statements that allow logical operations to be performed.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S1, S2 : String;

begin

  // String comparison
  Write('S1 = ');
  Readln(S1);
  Write('S2 = ');
  Readln(S2);

  // Operation =
  if S1 = S2 then
    Writeln('Strings are equal')
  else
    Writeln('Strings are not equal');

  // Operation <> (not equal)
  if S1 <> S2 then
    Writeln('Strings are not equal')
  else
    Writeln('Strings are equal');

  Readln;
end.

Program result

S1 = abcd
S2 = abcd
Strings are equal
Strings are equal

 

8. Operations >, <, >=, <=. Comparing strings for greater or lesser in terms of lexicographic order

To compare strings in lexicographic order, the operations >, >=, <, <= are used. In the most general case, the comparison of two strings S1 and S2 is as follows

S1 < S2
S1 <= S2
S1 > S2
S1 >= S2

The result of the operations is True or False. The lexicographic order of characters in strings is determined by the numerical value of the codes of these characters.

So, for example, the character code ‘A’ follows before the character code ‘B’. Accordingly, the expression

'A' < 'B'

will return the resulting value of True.

Example.

...

// String comparison
S1 := 'ABC';
S2 := 'ABCD';
res := S1 >= S2; // res = False
res := S1 > S2; // res = False
res := S1 <= S2; // res = True
res := S1 < S2; // res = True

...

 


Related topics