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 ValEdit

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}



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

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



The val function converts a string into a number only if the string doesn't contain non-numeric characters,otherwise the number turns to zero (tested with Bloodshed dev-pas). For example:

val (s,n,er); {n will be 123 and
               er will be 0, as no errors
               have occured during the
val (s,n,er); {n will be 0 because the
              conversion stopped when 'a'
              was found. thus, er will store
              the position of 'a' in the string.
              a will be 3}
val (s,n,er); {n will be 0 because the conversion
               stopped at the position of the
               first non-numeric character met.
               so er is 3 again}


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' }



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'}



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:

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



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

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

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.