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

How to delete an item from a dynamic (string) array

By Zarko Gajic, About.com

If you have a dynamic array of strings and want to delete one string from it, you can use the DeleteArrayItem procedure. Here's a sample usage

var
myArray: TStringArray;
...//delete fifth element
DeleteArrayItem(myArray, 5) ;

~~~~~~~~~~~~~~~~~~~~~~~~~
type
   TStringArray = array of string;

procedure DeleteArrayItem(var X: TStringArray; const Index: Integer) ;
begin
   if Index > High(X) then Exit;
   if Index < Low(X) then Exit;
   if Index = High(X) then
   begin
     SetLength(X, Length(X) - 1) ;
     Exit;
   end;
   Finalize(X[Index]) ;
   System.Move(X[Index +1], X[Index],(Length(X) - Index -1) * SizeOf(string) + 1) ;
   SetLength(X, Length(X) - 1) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» How to execute a method (procedure/function) by name
« How to draw transparent text on Windows Desktop

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. 2004 Delphi Tips
  7. How to delete an item from a dynamic (string) array

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

All rights reserved.