DelphiBasics
AnsiIndexStr
Function
Compares a string with a list of strings - returns match index StrUtils unit
 function AnsiIndexStr(const Source string; const StringList array of string):Integer;
Description
The AnsiIndexStr function checks to see if any of the strings in StringList exactly match the Source string.
 
When a match is found, its (0 based) index is returned. Otherwise, -1 is returned.
 
The string list can be specified as a square bracket delimited list, as in the example, or as an array of strings.
 
It is a Case sensitive command.
Related commands
AnsiMatchStrReturns true if a string exactly matches one of a list of strings
AnsiPosFind the position of one string in another
StrScanSearches for a specific character in a constant string
 Download this web site as a Windows program.




 
Example code : A simple example
var
  source : AnsiString;
  position : Integer;
begin
  source := 'Henry';  // The string to match

  // Note that AnsiIndexStr is case sensitive
  // We use a hard coded constant string array
  position := AnsiIndexStr(source, ['BRIAN', 'JIM', 'HENRY']);
  ShowMessageFmt('Index of first match attempt = %d',[position]);

  // Note that arrays start at 0
  position := AnsiIndexStr(source, ['Brian', 'Jim', 'Henry']);
  ShowMessageFmt('Index of second match attempt = %d',[position]);
end;
Show full unit code
  Index of first match attempt = -1
  Index of second match attempt = 2
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page