Home  |  Delphi .net Home  |  system.String  |  LastIndexOfAny Method
LastIndexOfAny  
Method  
Finds the last index of any of a set of characters within a string
String Class
system NameSpace
CF1.  Function LastIndexOfAny ( Values : Array of Char; ) : Integer ;
CF2.  Function LastIndexOfAny ( Values:Array of CharValues : Array of Char; Start : Integer; ) : Integer ;
CF3.  Function LastIndexOfAny ( Values:Array of CharValues : Array of Char; Start : Integer; Count : Integer; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns the index of the last occurence of any of the set of character Values in the current string.
 
If not found, then -1 is returned.
 
The scan is case sensitive.
 
The scan starts at the end and continues until either the Needle is found, or the string start is passed.
 
The scan can be forced to commence from the Start character backwards, and can be limited to Count scan characters.
Notes
Very Important : Methods in .Net treat strings as starting at 0, unlike traditional Delphi where they started at 1.
Microsoft MSDN Links
system
system.String
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

var
  strA     : String;
  strArray : Array[1..3] of Char;
  result   : Integer;

begin
  strA := 'Hello World';

  Console.WriteLine('Guide=           1111111111');
  Console.WriteLine('       01234567890123456789');
  Console.WriteLine('strA = ' + strA);

  strArray[1] := 'h';  // Not in the string
  strArray[2] := 'o';  // In positions 4 and 7 of the string
  strArray[3] := 'w';  // In position 11 of the string

  result := strA.LastIndexOfAny(strArray);
  Console.WriteLine('Looking for h,o,w');
  Console.WriteLine('Result = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  Guide=           1111111111
         01234567890123456789
  strA = Hello World
  Looking for h,o,w
  Result = 7
Specifying a start position
program Project1;
{$APPTYPE CONSOLE}

var
  strA     : String;
  strArray : Array[1..3] of Char;
  result   : Integer;

begin
  strA := 'Hello World';

  Console.WriteLine('Guide=           1111111111');
  Console.WriteLine('       01234567890123456789');
  Console.WriteLine('strA = ' + strA);

  strArray[1] := 'h';  // Not in the string
  strArray[2] := 'o';  // In positions 4 and 7 of the string
  strArray[3] := 'd';  // In position 11 of the string

  result := strA.LastIndexOfAny(strArray, 10);
  Console.WriteLine('Looking for h,o,d, starting @ 10 moving backwards');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.LastIndexOfAny(strArray, 6);
  Console.WriteLine('Looking for h,o,d, starting @ 6 moving backwards');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.LastIndexOfAny(strArray, 3);
  Console.WriteLine('Looking for h,o,d, starting @ 3 moving backwards');
  Console.WriteLine('Result = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  Guide=           1111111111
         01234567890123456789
  strA = Hello World
  Looking for h,o,d, starting @ 10 moving backwards
  Result = 10
  Looking for h,o,d, starting @ 6 moving backwards
  Result = 4
  Looking for h,o,d, starting @ 3 moving backwards
  Result = -1
Specifying a start position and limit in the character scan
program Project1;
{$APPTYPE CONSOLE}

var
  strA     : String;
  strArray : Array[1..3] of Char;
  result   : Integer;

begin
  strA := 'Hello World';

  Console.WriteLine('Guide=           1111111111');
  Console.WriteLine('       01234567890123456789');
  Console.WriteLine('strA = ' + strA);

  strArray[1] := 'h';  // Not in the string
  strArray[2] := 'o';  // In positions 4 and 7 of the string
  strArray[3] := 'd';  // In position 11 of the string

  result := strA.LastIndexOfAny(strArray, 10, 2);
  Console.WriteLine('Looking for h,o,d, starting @ 10 back for 2 chars');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.LastIndexOfAny(strArray, 6, 2);
  Console.WriteLine('Looking for h,o,d, starting @ 6 back for 2 chars');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.LastIndexOfAny(strArray, 3, 2);
  Console.WriteLine('Looking for h,o,d, starting @ 3 back for 2 chars');
  Console.WriteLine('Result = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  Guide=           1111111111
         01234567890123456789
  strA = Hello World
  Looking for h,o,d, starting @ 10 back for 2 chars
  Result = 10
  Looking for h,o,d, starting @ 6 back for 2 chars
  Result = -1
  Looking for h,o,d, starting @ 3 back for 2 chars
  Result = -1
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author