Deleteing from StringList

I have StringList that I need to delete some strings from.  I have a starting index number and and ending index number.  I want to delete those strings and any that are in-between them.  How can I do this?  I tried this:

         for i := iStart to iEnd do
         begin
               SavedDescriptions.Delete(i);
         end;

But I get a ListIndexOutOfBounds error.  

Currently, I'm just changing them all to blank, but I'd really like to have them deleted.

How can I delete the strings?
LVL 3
light_bulbAsked:
Who is Participating?
 
xr1140Commented:
try this:


         for i := iEnd downto iStart do
         begin
               SavedDescriptions.Delete(i);
         end;


everytime you delete an item your list become shorter.
0
 
2266180Commented:
or,
 for i := iStart to iEnd do
     SavedDescriptions.Delete(iStart);
:)

also, it is a good idea to do like this
SavedDescriptions.BeginUpdate;
try
 for i := iStart to iEnd do
     SavedDescriptions.Delete(iStart);
finally
  SavedDescriptions.EndUpdate;
end;

in order to speed things up, especially if the number of elements to delete is big.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.