我发现下面的链接,但它是C#: c# Check if all Strings in a String List are the Same.检查在字符串列表中的所有字符串在Inno Setup的同一
我写了工作守则,可以完成剩下的很好,但它只有使用Sorted String Lists才能正常运行。
问候,
function StringListStrCheck(const S: String; StringList: TStringList): Boolean;
var
CurrentString: Integer;
begin
if StringList = nil then
RaiseException('The StringList specified does not exist');
if StringList.Count = 0 then
RaiseException('The specified StringList is empty');
if StringList.Count = 1 then
RaiseException('The specified StringList does not contain multiple Strings');
Result := False;
CurrentString := 1;
Repeat
if (CompareStr(S, StringList.Strings[CurrentString]) = -1) then begin
Result := False;
end;
if (CompareStr(S, StringList.Strings[CurrentString]) = 0) then begin
Result := True;
end;
CurrentString := CurrentString + 1;
Until CurrentString > (StringList.Count - 1);
end;
返回true,如果指定的字符串是一样的,在指定的字符串列表中的所有其他字符串。
否则,它返回False。
但是,问题是只有在给定字符串列表已排序或其字符串没有空格的情况下,它才能正确执行检查。如果给定字符串列表中的任何字符串或所有字符串都有空格,则必须对其进行排序。否则,返回true,即使有非 - 等于串像Your Aplication
和Your ApplicationX.
例
这StringList的没有在任何场所任何字符串:
var
TestingList1: TStringList;
TestingList1 := TStringList.Create;
TestingList1.Add('CheckNow');
TestingList1.Add('DontCheckIt');
if StringListStrCheck('CheckNow', TestingList1) = True
then
Log('All Strings are the same');
else
Log('All Strings are not the same.');
它正确返回False,可以在日志的输出消息中看到。
这StringList的在其字符串空间:
var
TestingList2: TStringList;
TestingList2 := TStringList.Create;
TestingList2.Add('Check Now');
TestingList2.Add('Check Tomorrow');
TestingList2.Add('Dont Check It');
if StringListStrCheck('Check Now', TestingList1) = True
then
Log('All Strings are the same');
else
Log('All Strings are not the same.');
但是,在这里它返回True即使是那些字符串是不一样的。
但是,在我按如下所示进行排序后,该函数正常工作,并按预期返回False。
TestingList2.Sorted := True;
TestingList2.Duplicates := dupAccept;
我想知道为什么这个函数失败,如果给定的StringList的字符串有空格或给出的StringList没有排序,也想知道我怎样才能使这个功能,如果给定的StringList没有失败空格和/或给定的StringList没有排序。
在此先感谢您的帮助。
谢谢你,它运作良好..... – GTAVLover
忽略以前的评论。 – GTAVLover
你的建议让函数更好,但我仍然希望使用'Sorted = True'来处理StringLists,它们的字符串中有空格。 – GTAVLover