在TSTringlist中删除行

Gle*_*rse -1 delphi

你好我正在写一些字符串列表的值.并希望从字符串列表中删除一个值.

目前我写这样的字符串列表.

   FGamePlay.Locations.strings[0] := ('NumberOfLocations='+inttostr(NOL+1));   //add one to total
   FGameplay.Locations.Add(inttostr(Position.x)+inttostr(Position.Y)+'=pos');  //add the location to list
Run Code Online (Sandbox Code Playgroud)

这将返回一个像这样的列表

INDEX    VALUE
[0]       NumberOfLocations=4
[1]       23=pos
[2]       34=pos
[3]       24=pos
[4]       52=pos
Run Code Online (Sandbox Code Playgroud)

现在我试着像这样删除它

FGamePlay.Locations.Delete(FGamePlay.Locations.IndexOf(inttostr(ePosition.x)+inttostr(ePosition.Y)));
Run Code Online (Sandbox Code Playgroud)

是ePosition.x + ePosition.Y将等于23,34,24或52.因此它应该删除该行,但是当我添加此删除行时,我得到索引超出范围-1.我确实在此行之前停止了代码并查看了Locations()并且其中包含了所有这些数字.还看了epostion和X,Y值是34,因此也是正确的.任何的想法?谢谢格伦

RRU*_*RUZ 6

当您使用该IndexOf函数时,您必须传递确切的字符串才能查找,在这种情况下,因为您以这种方式添加字符串

FGameplay.Locations.Add(inttostr(Position.x)+inttostr(Position.Y)+'=pos'); 
Run Code Online (Sandbox Code Playgroud)

您必须将=pos字符串添加到要搜索的字符串中,例如

LIndex:=FGamePlay.Locations.IndexOf(inttostr(ePosition.x)+inttostr(ePosition.Y)+'=pos');
If LIndex>=0 then
 FGamePlay.Locations.Delete(LIndex);
Run Code Online (Sandbox Code Playgroud)

  • 这仍然是可怕的代码.帮助OP出局的+1,但对于迈克的爱,请格伦,不要串行输入你所做的一切. (2认同)