首页 > 解决方案 > 如何附加到 Tstringlist 中的给定字符串

问题描述

我通过将每封新电子邮件附加到现有的 csv 字符串来创建实现为 TStringlist 中的 csv 字符串的电子邮件地址列表。如果 MyStringlist[0] 中的 csv 字符串已经包含我要添加的电子邮件,那么我会将其附加到 MyStringlist[1]。如果该字符串也已经包含它,那么我会将它附加到 MyStringlist[2] 等等,直到找到一个不包含它的字符串。

例如,如果 MyStringlist[0] 和 MyStringlist[1] 都包含我要附加的电子邮件,但 MyStringlist[2] 尚不存在,因此无法附加到,则会出现问题。

例如,我可能需要将“a.co.uk”添加到已经包含以下字符串的 MyStringlist。

MyStringlist[0] -> a.co.uk, f.co.uk, h.co.uk, k.co.uk
MyStringlist[1] -> d.co.uk, a.co.uk, g.co.uk

我创建了一个过程来将电子邮件附加到给定索引处的字符串或在必要时在字符串列表中创建一个新字符串。

这段代码是正确的方法,还是我应该使用更简单的方法来执行此操作 - 或者以更健壮的方式?

创建的程序

procedure AppendToStringListItem(TheStringList: Tstringlist;
                                 Index : integer;
                                 StringToAppend : string);
var i : integer;
begin
if Index >= 0 then
  begin
  if TheStringList.count -1 < index then //not enough strings
    begin
    for i := 1 to index - TheStringList.Count  do  //add a blank string
       TheStringList.add('');
    TheStringList.add( StringToAppend); //add new string at position 'index'
    end
  else //just append
     TheStringList[Index] :=  TheStringList[Index] + ',' + StringToAppend
  end;
end;

标签: delphiappendtstringlist

解决方案


没有内置函数来确保TStrings/TStringList具有最少数量的字符串。所以是的,您必须一次手动添加一个字符串。

我会建议更像这样的东西:

procedure AppendToStringListItem(TheStringList: TStrings;
                                 Index : integer;
                                 StringToAppend : string);
var
  s: string;
begin
  if Index < 0 then Exit;
  TheStringList.BeginUpdate;
  try
    while Index >= TheStringList.Count do begin
      TheStringList.Append('');
    end;
    s := TheStringList[Index];
    if s = '' then 
      s := StringToAppend
    else
      s := s + ',' + StringToAppend;
    TheStringList[Index] := s;
  finally
    TheStringList.EndUpdate;
  end;
end;

但是,这确实需要您提前知道所需的索引,这意味着TStringList事先搜索,例如:

function AlreadyExistsInStringListItem(const S, StringToAppend: string): Boolean;
begin
  Result := ...;
end;

function FindIndexToAppendToStringListItem(TheStringList: TStrings;
                                           StringToAppend : string);
begin
  for Result := 0 to TheStringList.Count-1 do
  begin
    if not AlreadyExistsInStringListItem(TheStringList[Result], StringToAppend) then
      Exit;
  end;
  Result := TheStringList.Count;
end;

procedure AppendToStringListItem(TheStringList: TStrings;
                                 Index : integer;
                                 StringToAppend : string);
var
  s: string;
begin
  if Index < 0 then Exit;
  TheStringList.BeginUpdate;
  try
    while Index >= TheStringList.Count do begin
      TheStringList.Append('');
    end;
    s := TheStringList[Index];
    if s = '' then 
      s := StringToAppend
    else
      s := s + ',' + StringToAppend;
    TheStringList[Index] := s;
  finally
    TheStringList.EndUpdate;
  end;
end;
Index := FindIndexToAppendToStringListItem(TheStringList, 'a.co.uk');
AppendToStringListItem(TheStringList, Index, 'a.co.uk');

根据您的代码设计,这可能会浪费开销。您可以通过合并搜索和插入来简化该逻辑,例如:

function AlreadyExistsInStringListItem(const S, StringToAppend: string): Boolean;
begin
  Result := ...;
end;

procedure AppendToStringListItem(TheStringList: TStrings;
                                 StringToAppend : string);
var
  i : integer;
  s: string;
begin
  TheStringList.BeginUpdate;
  try
    for i := 0 to TheStringList.Count-1 do
    begin
      s := TheStringList[i];
      if not AlreadyExistsInStringListItem(s, StringToAppend) then
      begin
        TheStringList[i] := s + ',' + StringToAppend;
        Exit;
      end;
    end;
    TheStringList.Append(StringToAppend);
  finally
    TheStringList.EndUpdate;
  end;
end;
AppendToStringListItem(TheStringList, 'a.co.uk');

推荐阅读