【问题标题】:Check if all Strings in a String List are the Same in Inno Setup检查字符串列表中的所有字符串是否在 Inno 设置中都相同
【发布时间】:2016-08-21 07:53:56
【问题描述】:

我找到了以下链接,但它是针对 C# 的: c# Check if all Strings in a String List are the Same.

我写了一个工作代码,它可以很好地完成其余的工作,但它只能在排序字符串列表中正常工作。

问候,

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。

但是,问题在于,如果给定的字符串列表已排序或其字符串没有空格,则它只能正确执行检查。如果给定字符串列表中的任何字符串或所有字符串都有空格,则必须对其进行排序。否则,即使有不相等的字符串,例如 Your AplicationYour ApplicationX.,这也会返回 True

示例

此 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,可以在Log中的Output Message中看到。

这个 StringList 在它的 Strings 中有空格:

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 未排序。

提前感谢您的帮助。

【问题讨论】:

    标签: inno-setup pascalscript tstringlist


    【解决方案1】:

    只需在循环之前将Result 设置为True

    一旦任何字符串不匹配,在循环中将其设置为False

    Result := True;
    CurrentString := 1;
    Repeat
      if (CompareStr( S, StringList.Strings[CurrentString]) <> 0) then begin
        Result := False;
        Break;
      end;
    
      CurrentString := CurrentString + 1;
    Until CurrentString > (StringList.Count - 1 );
    

    如果您想忽略前导和尾随空格,请使用Trim

    【讨论】:

    • 忽略之前的评论。
    • 你建议把函数做得更好,但我还是想用Sorted = True来处理StringLists中有空格的。
    • Sorted 与空格有什么关系?代码并不关心字符串列表是否已排序。
    • 没有没有。它们是不同的。我的意思是,如果我想检查多个相同字符串的 StringList 在 其任何字符串 中有空格,则需要对 StringList 进行排序以使该函数正常工作。你可以检查一下。我之所以这么说,是因为在函数中添加了您的建议和新行 if StringList &lt;&gt; nil then StringList.Sorted := True; 后,这非常有效。检查一下。是真的!
    • 你说的也对。 Spaces 和Sorted 之间没有任何关系......我也想不出为什么会这样......你可以通过创建一个包含空格的字符串列表来尝试并理解这个问题它的字符串并尝试使用此函数对其进行检查.........如果您的 StringList 不是Sorted.,它一定会失败,相信我并尝试......
    【解决方案2】:

    您可以尝试使用您的字符串进行检查,例如 '"INSIDE DOUBLE QUOTATION MARKS"'。

    【讨论】:

    • 我还没查,我会告诉你发生了什么。我不认为它会起作用。
    猜你喜欢
    • 2014-04-09
    • 2018-02-09
    • 2013-01-09
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2014-01-01
    相关资源
    最近更新 更多