Back


Lennie's Weekly Tip: LeftStr & RightStr functions (22/03)
22 March Ref. 17

I'ved written the following two string-functions that you can use to return the left or right postion of a string. I used these functions when I was still programming in QBasic, but now I can also use them in Delphi programming.

LeftStr function:
=============

function LeftStr (const S: String; Count: Integer): String;
begin
  LeftStr := Copy(S, 1, Count);
end;

Remark:
This function will return a part of a string from the left postion.
The S parameter is a string-expression and the Count parameter will specify the
part of the string that must be returned.

Example:

Label1.Caption := LeftStr('Joanni', 4);    // Label1.Caption will return "Joan"
Label1.Caption := LeftStr('Joanni', 2);    // Label1.Caption will return "Jo"
 

RightStr function:
==============

function RightStr(const S: String; Count: Integer): String;
begin
  Result := Copy(S, Length(S) - Count + 1, Count);
end;

Remark:

This function will return a part of a string from the right.

The S parameter's a string-expression and the Count parameter specify the part of the string that must be return from the right side.

If the count-parameter is larger than the string represent by the S string parameter, the function returns the complete string.

Example:

Label1.Caption := RightStr('Joanni', 3);    // Label1.Caption will return "nni"
.
  .
Label1.Caption := RightStr('Joanni', 7);   //  If we increase the number to 7 which is a higher number than what is in the string, then it will return "Joanni"


Back