Borland Pascal Wiki
Borland Pascal Wiki
This article is part of the Getting Started series
 This page explains a basic Pascal instruction. If you want to see how to make your own instructions, see Procedures and Functions.

Character strings can be supposed to various operations. These operations are in fact functions that are included in the Borland dialect. They're found both in Borland Pascal and Free Pascal.

Str and Val[]

The str and the val function convert a number into a string and back.

str (a,s); {converts the a number variable into a string 
            and places it in the s string variable}
val (s,a,er); {converts the s string variable into the a number variable and
               if there is an error, er will contain the 
               position in the string where the error occured}

STR[]

str(from_number,to_string);

The str function converts a number (usually integer) into a character string. For example:

a:=12345;
str (a,s); {s will be '12345'}

VAL[]

val(from_string,to_number,error_integer);

The val function converts a string into a number until the first non-numeric character in the string, stores it in to_number and publishes the position of the possible non-numeric character in error_integer. If already the first character is non-numeric, to_number is 0 and error_integer is 1. from_string is not manipulated in any case. (Tested in all Object Pascal versions (Borland, Inprise, Embarcadero) from Delphi 5 to XE10.)

s:='123';
val (s,n,er); {n will be 123 and
               er will be 0, as no errors
               have occured during the
               conversion}
s:='98a';
val (s,n,er); {n will be 98 because the
              conversion stopped when 'a'
              was found. Thus, er will store
              the position of 'a' in the string.
              a will be 3}
s:='128a7b';
val (s,n,er); {n will be 128 because the conversion
               stopped at the position of the
               first non-numeric character met.
               So er is 4 here}

DELETE[]

delete (string_variable,from_pos,number_of_characters);

The delete procedure deletes a substring from a string by specifying the position from which the deletion of string will start and the number of characters in a string need to be deleted. The deleted substring can not be recorvered so it's lost forever. For example:

s := 'Honest Abe Lincoln';
delete (s,8,4);
writeln (s); { s will be 'Honest Lincoln' }

COPY[]

a:=copy(s_string,x,y);

The copy function returns the substring from the main string that starts on on the x position and has y characters. This is useful when you want to delete a substring from a string but also keep the deleted substring for later use in strings. For example:

s:='Honest Abe Lincoln';
x:=copy (s,8,3); {x will be 'Abe'}

POS[]

x:=pos(substring,string);

The pos function returns the position of a substring in a main string. If the substring does not exist in the main string, then the returned value will be 0. For example:

s:='note-book';
x:=pos('book',s); {x will be 6}

LENGTH[]

length(s_string);

The length function simply returns the length of the specified string. For example:

s:='abc';
x:=length(s); {x will be 3}
s:='';
x:=length(s); {x will be 0}