-1

I found the following link, but it is for C#: c# Check if all Strings in a String List are the Same.

I wrote a working Code which can do the rest well but it only function correctly with Sorted String Lists.

Regards,

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;

This returns True if the Specified String is same as all other Strings in the specified String List.

Otherwise, it returns False.

But, the problem is that it can only do the Checking correctly if the given String List is sorted or its strings don't have spaces. If any of the strings or all strings in the given String List have spaces, it must need to be Sorted. Otherwise this returns True even there are non - equal strings like Your Aplication and Your ApplicationX.

EXAMPLE

This StringList doesn't have any spaces in its any of the Strings:

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.'); 

It returns False correctly and it can be seen in the Output Message in the Log.

This StringList have spaces in its 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.'); 

But, here it returns True even those Strings are not the same.

But, after I sort it like below, the function works properly and returns False as expected.

TestingList2.Sorted := True;
TestingList2.Duplicates := dupAccept;

I like to know why this function is failing if the given StringList's Strings have spaces or given StringList is not Sorted and also like to know how can I make this function not to fail if the given StringList have spaces and /or given StringList is not sorted.

Thanks in Advance for your Help.

Community
  • 1
  • 1
GTAVLover
  • 1,407
  • 3
  • 22
  • 41

2 Answers2

2

Just set the Result to True before the loop.

And in the loop set it to False once any string does not match.

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 );

Use Trim if you want to ignore leading and trailing spaces.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Ignore previous comment. – GTAVLover Aug 21 '16 at 09:59
  • You recommendation make the function more good, but I still want to use `Sorted = True` to deal with StringLists those have spaces in their Strings. – GTAVLover Aug 21 '16 at 10:04
  • What has `Sorted` to do with spaces? The code does not care if the string list is sorted or not anyway. – Martin Prikryl Aug 21 '16 at 11:05
  • No no. they are different. I mean, if the StringList I want to be checked for multiple same strings has spaces in **its any of the strings** the StringList need to be Sorted for that function to work properly. You can check it . I tell because this worked perfectly after adding your recommendations and new line `if StringList <> nil then StringList.Sorted := True;` in the function also. Just check. It's true! – GTAVLover Aug 21 '16 at 11:11
  • You're also right . There are no any relationships between Spaces and `Sorted`......I also can't think why that's happening........You can try it and understand that problem by creating a String List with spaces in its Strings and trying to check it using this function.........It must fail if your StringList is not `Sorted.` Just believe me and try...... – GTAVLover Aug 21 '16 at 11:23
-1

You can try using your string to be checked like '"INSIDE DOUBLE QUOTATION MARKS"'.

Blueeyes789
  • 543
  • 6
  • 18