/ / String operator in Pascal - string

String operator in Pascal - string

Программирование постоянно связано с работой с some data, in particular, with strings. The string operator is set the same in most languages, including Pascal: string. But Pascal has its own characteristics and properties that you need to know before you begin.

What is a string in pascal?

Before Understanding the Syntaxstring data type in Pascal, it should be understood that may 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

Maximum number of characters per linemakes 255 units, and each of them receives the corresponding sequence number. Therefore, if you need to write large text into a variable, you should create an array of string. Pascal will correctly process this request, and it will be possible to save much longer text data.

String data type

For strings is the String type.Pascal allows the programmer to specify the exact number of characters or to leave its length by default - in this case it will be equal to 255. To declare a string variable, listing the same arguments, you must enter the keyword after the colon - string and, if necessary, specify the length in square brackets lines. In the example below, the string is "str", 10 characters long:

var str: string [10].

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

String operations

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

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

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

string pascal

Another operation supported in Pascal is string compare or string comparison. It also uses the simplest mathematical signs:

  • equality (=);
  • over / under (> and <);
  • inequalities (<>);
  • and also greater or equal and less than or equal (> = and <=).

The result of the relation operation is the return of a boolean true or false.

String comparison is performed character by character, andfinding the first inconsistency result will be determined according to the coding table. So, when figuring out which row is larger, at the position where there will be different characters, the code in this table will be compared, and the result will determine the answer that the operation returns.

how to convert string integer pascal

Functions with string variables

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

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

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

  • 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 are used, which must be combined:

Concat (s1, s2 ...).

  • Often used in Pascal isLength 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 - the user will receive an integer value at the output:

Length (str).

  • And the last of the functions in Pascal is searchthe beginning of the substring in the string - Pos. It returns the number of the character from which the required substring begins, and in case of its absence, the result of the 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 row of characters into the string.

type string pascal

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

Do it (With, pos, n).

And insert a sequence of characters in the stringYou can use the Insert. The procedure has as its parameters three values ​​— a substring, a string, and a position from which the characters will be inserted:

Insert (subS, S, poz).

Changing data type from strings

When performing tasks, often have to changetype of variables. Consider, for example, how to convert a string to an integer. Pascal does not allow adding the numbers written in the string, therefore, to produce a sum, their type must be changed. There are special procedures for this:

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

array string pascal

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