Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Strings, Numbers and Text

How to operate with Strings by svanels
Posted: 14 May 03 (Edited 13 Aug 05)

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


  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


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
   Outstring := ';
   for j:1 to length(instring) do
    Outstring := Outstring + Upcase(instring[j]);
    result := Outstring;

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


Mainstring := 'This is the Borland Turbo Pacal Compiler';

Substring1 := 'Turbo pascal';

Substring2 := 'Turbo Pacal';

i := Pos( SubString1, MainString);

Returns i = 0;

i := Pos( SubString2, MainString);

Returns i = 21;


This procedure inserts a Source in the Target string at the position Index

Target := 'www.goto.com';
Source := 'http://';
Index := 1;

Insert ( Source , Target , Index);   
 results in Target --> 'http://www.goto.com'

  the index starts at 1 and not at 0, contrary to the sequence numbering used in items of delphi objects

This procedure removes the quantity of characters specified in L from the string target, starting
at the Index given

Target := 'black +and white';
L := 3;
Index := 8;

Delete ( Target, Index, L);

Target --> 'black + white'


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]:Decimals] , TextVal)   which converts the value in number to text and stores it in Textval

Number = 235.489
STR (Number:3:1) --> 235.4


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


    Textval := '354';
    Val(Textval, Number, Code)  ---> Code = 0, Number = 354

   Textval := '37645x2';
   Val( Textval, Number, Code)  ---> Code = 6,  Number remains unchanged;

This FAQ was originally posted in the extinct Turbo Pascal Forum

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close