Wikia

Borland Pascal Wiki

String operations

Talk0
35pages on
this 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 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}

STREdit

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

VALEdit

val(from_string,to_number,error_integer);

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:

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 0 because the
              conversion stopped when 'a'
              was found. thus, er will store
              the position of 'a' in the string.
              a will be 3}
s:='98a7b';
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}

DELETEEdit

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

COPYEdit

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

POSEdit

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}

LENGTHEdit

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}

Around Wikia's network

Random Wiki