[英]How to append to a given string in a Tstringlist
I'm creating lists of email addresses implemented as csv strings in a TStringlist by appending each new email to an existing csv string.我正在通过将每个新的 email 附加到现有的 csv 字符串来创建 email 地址列表,该地址在 TStringlist 中实现为 csv 字符串。 If the csv string in MyStringlist[0] already contains the email I'm about to add then I'll append it to MyStringlist[1].
如果 MyStringlist[0] 中的 csv 字符串已经包含我要添加的 email,那么我将 append 将其添加到 MyStringlist[1]。 If that string also already contains it then I'll append it to MyStringlist[2] and so on until I find a string that doesn't contain it.
如果该字符串也已经包含它,那么我将 append 到 MyStringlist[2] 等等,直到找到一个不包含它的字符串。
The problem occurs if, for example, MyStringlist[0] and MyStringlist[1] both contain the email I want to append but MyStringlist[2] doesn't yet exist so it can't be appended to.例如,如果 MyStringlist[0] 和 MyStringlist[1] 都包含 email 我想要 append 但 MyStringlist[2] 尚不存在,因此无法附加到,则会出现问题。
eg I might need to add 'a.co.uk' to MyStringlist that already contains the strings below.例如,我可能需要将“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
I created a procedure to append an email to a string at a given index or create a new string in the stringlist if necessary.我为给定索引处的字符串创建了 append 和 email 的过程,或者在必要时在字符串列表中创建新字符串。
Is this code the correct way to do it or are there functions I should use to do this in a simpler way - or maybe in a more robust way?这段代码是正确的方法,还是我应该使用更简单的方法来执行此操作 - 或者以更健壮的方式?
Procedure created创建的程序
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;
There is no built-in function to ensure a TStrings
/ TStringList
has a minimum number of strings.没有内置的 function 来确保
TStrings
/ TStringList
具有最少的字符串数。 So yes, you will have to add strings manually one at a time.所以是的,您必须一次手动添加一个字符串。
I would suggest something more like this:我会建议更像这样的东西:
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;
However, this does require you to know the desired index ahead of time, which means searching the TStringList
beforehand, eg:但是,这确实需要您提前知道所需的索引,这意味着事先搜索
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');
Depending on your code's design, that could be wasted overhead.根据您的代码设计,这可能会浪费开销。 You could simplify that logic by merging the searching and inserting together, eg:
您可以通过合并搜索和插入来简化该逻辑,例如:
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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.