【发布时间】: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 Aplication 和 Your 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