2016-08-21 59 views
-1

我发现下面的链接,但它是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 AplicationYour 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没有排序。

在此先感谢您的帮助。

回答

2

只需在循环之前将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

+0

谢谢你,它运作良好..... – GTAVLover

+0

忽略以前的评论。 – GTAVLover

+0

你的建议让函数更好,但我仍然希望使用'Sorted = True'来处理StringLists,它们的字符串中有空格。 – GTAVLover

-1

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

+0

我还没有检查,我会通知你发生了什么事。我不认为它会起作用。 – GTAVLover

相关问题