delphi - Remove empty strings from TStringList -
is there build-in function in delphi remove strings tstringlist
empty?
how loop through list remove these items?
to answer first question, there no built in function that. looping manually easy. should it:
for := mylist.count - 1 downto 0 begin if trim(mylist[i]) = '' mylist.delete(i); end;
note loop must traverse list in reverse starting count-1 down 0 work.
the use of trim()
optional, depending on whether want remove strings contain whitespace or not. changing if
statement if mylist[i] = '' then
remove empty strings.
here full routine showing code in action:
procedure tmyform.button1click(sender: tobject); var i: integer; mylist: tstringlist; begin mylist := tstringlist.create; try // add random stuff string list := 0 100 mylist.add(stringofchar('y', random(10))); // clear out items empty := mylist.count - 1 downto 0 begin if trim(mylist[i]) = '' mylist.delete(i); end; // show remaining items numbers in list box := 0 mylist.count - 1 listbox1.items.add(inttostr(i)+' '+mylist[i]); mylist.free; end; end;
Comments
Post a Comment