Last Updated:

Delphi: Copy function with an example

Delphi is an object-oriented programming language that helps developers create a quality backend of a website. One of the necessary things that a programmer should know is the functions of working with strings, such as delphi copy, delete, pos and others.

Delphi: Copy function with an example

Why do I need to work with strings?

Having all the source code in front of your eyes, you can find the part of interest in a particular text without using special functions. Then do with this part any actions: create a new variable with this value, delete, replace, etc. If the text is small, then it is easy to do, but often on sites it is a huge number of characters.

To optimize your work, it is recommended to use special functions that will do all the monotonous work of finding the right substring for the programmer.

Most Requested Features

Length

In all functions of working with strings, you need to know its length. One of the parameters we specify the beginning and end of the copied fragment or the place where you want to paste the substring. To quickly calculate how many total characters are contained, you can use the length function.

x:=length ('inserted line text'); you get 24 characters, 2 spaces and they also count.

Pos

It will help when you need to find the position from which the desired word begins. For example, we have a 'string with characters' in which we need to find the letter "k". To quickly find out its position, let's write down:

x:=pos ('to', 'string with characters'); get the number 5

If what we are looking for is not contained here, then 0 will return.

Delete

If you need to delete part of the text, the delete function will come to the rescue. Its general structure is as follows:

delete ( [variable name with text], [digit of the character from which the deletion begins], [how many characters need to be deleted];

In practice, this function can be applied as follows. Suppose we have a site name from which we want to remove the initial "http://" characters. It is recorded as follows:

x:=’http://x.loc’;
delete (x, 1, 7);

First, create a variable with which we equate the value of the string with the name of the site. Then apply delete, specify that the deletion begins with the first character and you will need to remove 7 characters, starting from this position. As a result, the variable x will contain the domain name without the prefix.

Copy

By using copy, you can set a new variable to the value of a part of the string of an existing variable. For example, there is an x variable that contains "experienced programmer". We want to create a variable y that copies only the second word from x. The record is similar in structure to delete.

x:= 'experienced programmer';
y:= copy (x, 9, 11);

What other functions are there for strings?

The delphi language has other functions that allow you to:

  • translate numbers into a string and vice versa;
  • return the current date, time in various writing formats and convert them to a string;
  • convert text to uppercase or lowercase;
  • Remove spaces.
  • compare.