1. Home
  2. Computing & Technology
  3. Delphi Programming

Left, Mid, Right String

By Zarko Gajic, About.com

. LeftStr() takes a certain portion of the left side of a string.
. MidStr() takes a specified number of characters from a string.
. RightStr() takes a certain portion of the right side of a string.

Let's say we have a string Dstr := 'Delphi is the BEST', then

LeftStr(Dstr, 5) := 'Delph'
MidStr(Dstr, 6, 7) := 'i is th'
RightStr(Dstr, 6) := 'e BEST'

~~~~~~~~~~~~~~~~~~~~~~~~~
function RightStr
    (Const Str: String; Size: Word): String;
begin
  if Size > Length(Str) then Size := Length(Str) ;
  RightStr := Copy(Str, Length(Str)-Size+1, Size)
end;

function MidStr
    (Const Str: String; From, Size: Word): String;
begin
  MidStr := Copy(Str, From, Size)
end;

function LeftStr
    (Const Str: String; Size: Word): String;
begin
  LeftStr := Copy(Str, 1, Size)
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
p.s.
Or, if you have Delphi 6+, you an use the LeftStr, RightStr or the Copy RTL functions.

Delphi tips navigator:
» Reverse a String
« Enlarge a Form Over Screen Size

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 1999 Delphi Tips
  7. Left, Mid, Right String

©2009 About.com, a part of The New York Times Company.

All rights reserved.