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 |
AnsiMatchStr |
|
Returns true if a string exactly matches one of a list of strings |
AnsiPos |
|
Find the position of one string in another |
StrScan |
|
Searches for a specific character in a constant string |
|
|
|
Example code : A simple example |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses StrUtils, // Unit containing the AnsiIndexStr command Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); 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; end.
|
Hide full unit code |
Index of first match attempt = -1
Index of second match attempt = 2
|
|