Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to operate with Strings

Strings, Numbers and Text

How to operate with Strings

by  svanels  Posted    (Edited  )
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';

Substring1 := 'Turbo pascal';

Substring2 := 'Turbo Pacal';

i := Pos( SubString1, MainString);

Returns i = 0;


i := Pos( SubString2, MainString);

Returns i = 21;


----------------------------------------------------------------------------------

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

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


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

Delete ( Target, Index, L);

Target --> 'black + white'
-----------------------------------------------------------------------------------

Copy

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

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

example:

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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top