Last Updated:

Delphi string operations

The Delphi programming language library includes several procedures and functions that are useful enough to work with strings. What are the operations on Delphi strings? Let's dwell on each of them in more detail and give their examples.

Examples of delphi string operations

1) The length function returns the length of the specified string. This function has a single parameter, which is an expression that has a string data type. The value of the length function is an integer that indicates the number of characters that make up the string.





b:=length(‘na beregu’); 


As a result of these statements, the example variable a is set to 6, and the variable b is set to 9. 2)

2) Delete procedure. You can use the delete procedure to delete a specific part of a row. The general appearance of the delete procedure is as follows:


delete(string, n, m)


From where:

  • A string indicates a variable or constant with a string data type.
  • n is the character number from which the specified substring is removed;
  • m is the length of the substring that is subsequently removed.


When you execute the statement


st:= 'City of Yekaterinburg';




the variable st will take the value-string 'City'. 3)

3) Pos function. Using the pos function, it is possible to determine the position of a substring in a specified line. The general appearance of the pos function instruction is as follows:


pos(substring, string);


from where the substring is a string-type variable or constant that you want to find in the string variable or string constant.


When you run the following statement:




The variable n will take the value 2. If the string does not contain the substring you are looking for, pos returns 0. Here is an example of a while statement statement that will remove the leading spaces from the specified line:


while(pos(‘ ‘,s)=1) and (length(s) > 0) do




Here, the delete(s, 1, 1) procedure removes spaces as long as the space is the initial character of the string (that is, as long as the value of the pos(' ',s) function is the same as one). The length(s) criterion check > 0 is used here to account for the possibility of including only spaces in a string.

4) The copy function selects a fragment of the specified line. The general appearance of the copy function statement is as follows:


soru (string, n, m);


Where the string denotes a string expression that includes the string whose fragment you want to retrieve. n denotes the number of the first character from which the substring is highlighted; m indicates the length of the substring to be highlighted.


When you execute the statement


s:= 'City of Yekaterinburg';


town:=copy(s, 7, 12) ;   


the town variable will take the value-string 'Yekaterinburg'.