System.Classes.TStringList.Find

From Appmethod Libraries
Jump to: navigation, search

Object Pascal

function Find(const S: string; var Index: Integer): Boolean; virtual;

C++

virtual bool __fastcall Find(const System::UnicodeString S, int &Index);

Properties

Type Visibility Source Unit Parent
function public
System.Classes.pas
System.Classes.hpp
System.Classes TStringList

Description

Locates the index for a string in a sorted list and indicates whether a string with that value already exists in the list.

Use Find to obtain the index in a sorted list where the string S should be added. If the string S, or a string that differs from S only in case when CaseSensitive is false, already exists in the list, Find returns true. If the list does not contain a string that matches S, Find returns false. The index where S should go is returned in the Index parameter. The value of Index is zero-based, where the first string has the index 0, the second string has the index 1, and so on.

Note: Only use Find with sorted lists. For unsorted lists, use the IndexOf method instead.

Tip: If the S string is not found (thus return value of Find is False) then Index is set to the index of the first string in the list that sorts immediately before or after S.

var
  Index: Integer;
  LStringList: TStringList;

begin
  LStringList := TStringList.Create;
  LStringList.Add('first string');
  LStringList.Add('second string');

  LStringList.Find('first string', Index); // Index = 0 because 'first string' is at index 0
  LStringList.Find('third string', Index); // Index = 2 because 'third string' sorts after 'second string'
  LStringList.Find('great string', Index); // Index = 1 because 'great string' would sort after 'first string', if it existed

  LStringList.Free;
end;

See Also

Code Examples