This FAQ was originally posted in the extinct Turbo Pascal Forum. The functions posted here are tested up to Delphi 6. In Delphi 7 some name changes apeared, like pos changed to PosEx Also some of the other code snippets have been combined into "Delphi Functions" in later versions
Some of the most common operations with strings are already embedded in Delphi. Here is a list of some of the procedures and functions that are available
Length
Upcase
Pos
Insert
Delete
Copy
Val
Str
String, Length and Upcase
A string is a chain of characters in pascal or an array of characters.
To acces the individual elements of the string, we can use the standard array construction:
S := 'www.Tek-Tips.com';
S[7] ---> k
note The first character in the string is stored on position 1, so: S[1] ---> w unlike languages as C and C++ (Thanks Towerbase!). In position 0 information about the string length is stored
The function length returns the number of elements (characters)
in the string
Example
S := 'www.Tek-Tips.com';
length(S) --> 16
The procedure Upcase converts a character to his uppercase equivalent (works on characters)
Upcase(a) ---> A
Combining the 3 mentioned procedures/functions in a function for converting a string to uppercase:
Function Uppercase(Instring : string) : string;
var j: integer;
Outstring : string
begin
Outstring := ';
for j:1 to length(instring) do
Outstring := Outstring + Upcase(instring[j]);
result := Outstring;
end;
--------------------------------------------------------------------------- Pos
Sometimes we need to know if a certain word or phrase is part of a string
the function Pos returns the position of the substring in the analyzed string. If
the substring is not encountered the result is 0
example
Mainstring := 'This is the Borland Turbo Pacal Compiler';
obs:
the index starts at 1 and not at 0, contrary to the sequence numbering used in items of delphi objects
------------------------------------------------------------------------------------
Delete
This procedure removes the quantity of characters specified in L from the string target, starting
at the Index given
This function returns a part from the string target, with a length of L characters from the position specified in Index
Target := 'Turbo Pascal Compiler'
L := 6;
Index := 7;
substring := Copy (Target, Index, L );
will results in substring --> Pascal
------------------------------------------------------- STR, Val
At last there are the standard string to number, and number to string conversion:
STR( Number[:length]ecimals] , TextVal) which converts the value in number to text and stores it in Textval
Number = 235.489
STR (Number:3:1) --> 235.4
and
Val( TextVal, Number , Code) which converts String to a number.
if this is possible the result of code = 0, other wise code indicates the character where
the error occured
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.