Test if string is within a string list

Hi all.

I have 2 different string lists and I am wanting to test if all strings in one list are in the other one and if not, add the string to another list for results.
I am using the following routine:
//-------------------------------------------------------------
procedure TForm1.btnCheckClick(Sender: TObject);
var
i: Integer;
sStr: String;
begin
lbResults.Clear;
  for i := 0 to lbCheck.Items.Count -1 do
  begin
  Application.ProcessMessages;
  sStr := lbCheck.Items.Strings[i];
    if lbAgainst.Items.IndexOf(sStr) = -1 then
    begin
    lbResults.Items.Add(lbCheck.Items[i]);
    end;
  end;
MessageDlg('Checking Complete',mtInformation,[MBOK],0);
end;

//-----------------------------------------------------------------------------------------

It seems that the routine above is not working at all and is simply giving me the full list of items in the lbCheck list box.

Is there something simple I am missing here or even better, is there a better way to do this??

Cheers.
Colin..
LVL 4
BedouinDNAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LRHGuyCommented:
I tried your code and it seems to work fine.

Everything that was in list lbCheck and not in lbAgainst was copied to lbResult.

This also works:

begin
  lbResults.Clear;
  for i := 0 to lbCheck.Items.Count -1 do begin
    sStr := lbCheck.Items[i];
    if lbAgainst.Items.IndexOf(sStr) = -1 then begin
      lbResults.Items.Add(sStr); //use sStr, since it already has the value
    end;
  end;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BedouinDNAuthor Commented:
It did work with only a few strings in either list, however I now have about 7500 items in each list and the code is not handling it for some reason. I am getting a full list of all items in the lbCheck list box even though I know that there are duplicates in the lists.
:-(
0
LRHGuyCommented:
I tried it with lists of 8000 in lbCheck and lbAgainst. It still seems to work. I verified it by displaying the number of items in each list after running the check. The result list showed 4000 items.

You can probably get some speed by keeping the list sorted (Sorted:=True) and using a binary search. I wrote a simple one in 'find'. It seems to speed it up, although I didn't do any official timing.

function Find(aStr:String; aList:tStrings):Integer;
var
  C,H,L,R:integer;
  Fnd:boolean;
begin
  { Assumes then aList is sorted }
  C:=aList.Count;
  H:=C-1;
  L:=0;
  repeat
    R:=((H-L) div 2)+L;
    Fnd:=(aList[R]=aStr);
    if not Fnd then begin
      if aStr>aList[R] then
        L:=R+1
      else
        H:=R-1;
    end;
  until Fnd or (L>H);
  if Fnd then
    Result:=R
  else
    Result:=-1;
end;

procedure TForm2.Button2Click(Sender: TObject);
var
  i:Integer;
  sStr: String;
begin
  lbResults.Clear;
  for i := 0 to lbCheck.Items.Count -1 do begin
    sStr := lbCheck.Items[i];
    if
     {lbAgainst.Items.IndexOf(sStr) = -1}
     Find(sStr,lbAgainst.Items)=-1  //faster!
    then begin
      lbResults.Items.Add(sStr);
    end;
  end;
  label3.caption:=inttostr(lbResults.Items.Count);
end;
0
BedouinDNAuthor Commented:
Thanks for your help, it seems Monday morning has been too much for me.. :-(
Turns out there was trailing whitespace after the strings in one of the lists, so I have added a TRIM function to get rid of them and now the code works..
Thanks again for your time..

Colin.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.