String operator in Pascal - string

Programming is constantly associated with working with some data, in particular with strings. The string operator is specified identically in most languages, including Pascal: string. But Pascal has its own characteristics and properties that you need to know before starting work.

What is the string in Pascal?

Before you understand the syntax of a string data type in Pascal, you should understand what can be a string. In fact, in Pascal, a string is an array of characters, each of which can be any element from an ASCII table. That is, any letter, number, punctuation mark or space can be used as a string.

pascal string

The maximum number of characters in one line is 255 units, and each of them receives the corresponding serial number. Therefore, if you need to write large text in a variable, you must create an array of string. Pascal will correctly process this request, and it will be possible to save significantly longer text data.

String data type

String is responsible for strings. Pascal allows the programmer to specify the exact number of characters or leave its default length - in this case it will be 255. To declare a string variable by listing the same arguments, you must specify the keyword after the colon - string and, if necessary, specify the length in square brackets strings. The following example sets the string "str" ​​to a length of 10 characters:

var str: string [10].

In the Pascal string program code, you can specify any values ​​- for this you only need to enclose them in single quotes.

String Operations

Depending on the language, the number of operations with which the string can work is also determined. Pascal allows you to compare and merge string data.

A merge operation (in other words, concatenation or concatenation) is performed to combine several lines into one. It is implemented using the addition sign: "+". Using it, you can build a single line consisting of a series of expressions, constants and variables.

It is worth noting that when combining words into a sentence by concatenating strings, there will be no space between each element. Therefore, in order to get a competently built structure, you should explicitly indicate the presence of a gap in the right places by adding a space character, for example, like this: "".

pascal string

Another operation supported by Pascal is string compare or string comparison. The simplest mathematical signs are also used for it:

  • equality (=);
  • more / less (> and <);
  • inequalities (<>);
  • as well as greater than or equal to and less than or equal to (> = and <=).

The result of the relationship operation is to return a boolean value of true or false.

Comparison of strings is done character by character, and when the first discrepancy is found, the result will be determined according to the encoding table. So, when finding out which line is larger, at the position where the different characters will be, the code will be compared in this table, and the results will be determined by the result that the operation will return.

how to convert string to integer pascal

Functions with String Variables

As with any programming language, Pascal has a number of functions with which a string can be used. Pascal allows you to copy part of a variable, combine several lines between each other, find a substring and calculate its length. This is done using the following 4 functions:

  • The Copy function is responsible for copying part of the string. It contains three parameters - the string or variable name, the position of the beginning and the number of characters to be copied:

Copy (S, poz, n) - here S is a string variable, and poz and n are integers.

  • In addition to concatenating strings using the β€œ+” symbol, you can perform this operation in a more convenient way using the Concat function. As its arguments, all strings and character expressions that must be combined are used:

Concat (s1, s2 ...).

  • A function often used in Pascal is Length. With its help, you can calculate the length of the string - that is, find out the number of characters in it. Its only argument is the string itself - at the output, the user will receive an integer value:

Length (str).

  • And the last of the functions in Pascal is to search for the beginning of a substring in a string - Pos. It returns the number of the character with which the desired substring begins, and if it is absent, the result of execution is 0:

Pos (subS, S).

Procedures for strings in Pascal

There are only two standard procedures used in Pascal. The first allows you to remove some substring, and the second - insert a series of characters into the string.

type string pascal

So, the Delete procedure removes a substring from a certain number of characters from the selected line, from the specified position. Each of these parameters is an argument of this operation:

Delete (S, poz, n).

And you can insert a sequence of characters into a string using Insert. The procedure has three values ​​in the role of parameters - a substring, a string, and a position, starting from which the characters will be inserted:

Insert (subS, S, poz).

Change data type from strings

When performing tasks, it is often necessary to change the type of variables. Consider, for example, how to convert string to integer. Pascal does not allow you to add the numbers written in a string, therefore, to produce the sum, their type must be changed. There are special procedures for this:

  • To convert string to integer in Pascal, you must use the StrToInt procedure. The obtained integer value can be written to a variable, and mathematical operations can be performed with it.
  • If you need to get a floating-point number from a given string, use the StrToFloat procedure. As with integers, the result of its execution can be used immediately.

array string pascal

  • To perform the inverse operations - convert numbers to strings - you must use the procedures FloatToStr for floating-point numbers and IntToStr for integer values.
  • Another way to change the type is to use special procedures for which you do not need to know the initial or final data type - Str and Val. The first one translates from a number to a string and takes two values ​​as an argument - the initial number and a variable with the name of the string. The second one performs the inverse operation, and has one more parameter - in addition to the numeric and string values, the procedure provides a code that informs about the correctness of the conversion. So, for example, trying to write a fractional number to an integer variable, the code will show the number of the character on which the failure occurred, and if the conversion is correct, its value will be 0.


All Articles