解决方法
要回答你的第一个问题,没有内置功能.手动循环很容易.这应该这样做:
for I := mylist.count - 1 downto 0 do begin if Trim(mylist[I]) = '' then mylist.Delete(I); end;
请注意,for循环必须反向遍历列表,从Count-1开始向下移动到0才能使其生效.
Trim()的使用是可选的,具体取决于您是否要删除仅包含空格的字符串.将if语句更改为if mylist [I] =”然后将仅删除完全空字符串.
procedure TMyForm.Button1Click(Sender: TObject); var I: Integer; mylist: TStringList; begin mylist := TStringList.Create; try // Add some random stuff to the string list for I := 0 to 100 do mylist.Add(StringOfChar('y',Random(10))); // Clear out the items that are empty for I := mylist.count - 1 downto 0 do begin if Trim(mylist[I]) = '' then mylist.Delete(I); end; // Show the remaining items with numbers in a list Box for I := 0 to mylist.count - 1 do ListBox1.Items.Add(IntToStr(I)+' '+mylist[I]); finally mylist.Free; end; end;