Last Updated:

Delphi strings

Delphi strings can be represented by several types: Longstring, widestring, and shortstring. The main differences between these types are the maximum allowed string length, the way you allocate memory for variables, and how you encode the characters.

For a variable of type shortstring, memory is allocated before the main program starts running, and the length of such a string cannot exceed the value of 255 (the number of characters). For variables of data type Longstring andwidestring, memory is allocated not before the start, but during the program operation, as a result of which such strings can have a practically unlimited length.

In addition to the above string types, you should use another fairly generic string string data type, equivalent to the Shortstring data type. As with a character type, a variable with a string data type must be declared in the var section. In this case, the Delphi string declaration statement is represented as follows:

 

name: string; 

 

or

 

name: string [long];

 

From where:

name — acts as the name of the variable;

 

string – a special keyword denoting a string type;

long is an integer data type constant that specifies the maximum possible string length.

Examples of delphi string declarations:

 

s: string; st: string [55];

 

In the first example, as we can see, the length of the string is not specified, so its length should be considered equal to 255 characters:

 

s: string;  

or

 

s: string [255];

 

These string variable declarations are equivalent to each other. If you want to specify a string (a string constant) in the source code of a program that is a sequence of characters, you must enclose the string in single quotation marks. For example, to assign a variable of string type name to some value, you should write:

 

password:= ‘taina’; 

 

or

 

password:= ‘1234’;

 

Please note that the ad instructions

 

password:= 1234;

 

incorrect because there is no correspondence between the type of the constant and the type of the variable. During the compilation of this instruction, a special message will be displayed:

 

incompatible types: ‘Char’ and ‘Integer’

 

which means that the Char and Integer data types are incompatible. Through operations

 

=,   <,   >,   <=,   >=

 

it is possible to compare one variable of type string with another variable of string type, or with a string constant. In this case, the comparison of the specified strings passes through each character, starting with the first. If the strings include exactly the same characters, then such strings are equal. If there are different characters in the same position in both lines, then the larger line is the one in which there is a character in this position that has a larger code. The following is a table with examples of row comparisons.

  1. str1='Leonov', str1='Leonov' — lines are equal to
  2. str1='Leonova', str1='Leonov' — str1 more str2
  3. str1='Leonov', str1='Leonova' — str1 less than str2

An addition operation applies to Delphi strings, which results in a new row. For example, when you follow these instructions:

 

im:= ‘Lera  ‘; 

 

fam:= ‘Ivanova’; 

fi:= im + fam;

 

the fio variable will be set to 'Lera Ivanova'.