TStringList.Find

From Soldat Community Wiki
Jump to: navigation, search
function Find(S: String; var Index: Integer): Boolean
 S: string to be found (or string to be found index for)
 Index: main result of the function, index of Strings[] that S is or would be added
 Result: TRUE if S has been found, FALSE otherwise

Description

Find returns True if the string S is present in the list. Upon exit, the Index parameter will contain the position of the string in the list. If the string is not found, the function will return False and Index will contain the position where the string will be inserted if it is added to the list.
Note: this function is supposed to be used with sorted lists. For unsorted list use IndexOf.

Example

var
  s: TStringList;
  i: integer;

begin
  s := File.CreateStringList();
  s.Append('a'); // index 0
  s.Append('b'); // index 1
  s.Append('d'); // index 2
  s.Sort();
  s.Sorted := TRUE;

  s.Find('b',i); 
  // i = 1

  s.Find('c',i);
  // i = 2

  if s.Add('c') = i then
    WriteLn('Indeed added at index '+inttostr(i))
  else
    WriteLn('Oops there''s a bug!');

  s.Free;
end.