Home  |  Delphi .net Home  |  System.String  |  LastIndexOf Method
LastIndexOf  
Method  
Finds the last index position of a string within a string
String Class
System NameSpace
CF1.  Function LastIndexOf ( Needle : Char; ) : Integer ;
CF2.  Function LastIndexOf ( Needle:CharNeedle : Char; Start : Integer; ) : Integer ;
CF3.  Function LastIndexOf ( Needle:CharNeedle : Char; Start : Integer; Count : Integer; ) : Integer ;
CF4.  Function LastIndexOf ( Needle : String; ) : Integer ;
CF5.  Function LastIndexOf ( Needle:StringNeedle : String; Start : Integer; ) : Integer ;
CF6.  Function LastIndexOf ( Needle:StringNeedle : String; Start : Integer; Count : Integer; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns the last index of the Needle string in the current (haystack) 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;
  result : Integer;

begin
  strA := 'ABCDEABCDE';

  Console.WriteLine('Guide= 0123456789');
  Console.WriteLine('strA = ' + strA);

  result := strA.LastIndexOf('AB');
  Console.WriteLine('Looking for AB');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.LastIndexOf('BA');
  Console.WriteLine('Looking for BA');
  Console.WriteLine('Result = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  Guide= 0123456789
  strA = ABCDEABCDE
  Looking for AB
  Result = 5
  Looking for BA
  Result = -1
Specifying a start position
program Project1;
{$APPTYPE CONSOLE}

var
  strA   : String;
  result : Integer;

begin
  strA := 'ABCDEABCDE';

  Console.WriteLine('Guide= 0123456789');
  Console.WriteLine('strA = ' + strA);

  result := strA.LastIndexOf('AB', 6);
  Console.WriteLine('Looking for AB, starting @ 6');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.LastIndexOf('AB', 5);
  Console.WriteLine('Looking for AB, starting @ 5');
  Console.WriteLine('Result = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  Guide= 0123456789
  strA = ABCDEABCDE
  Looking for AB, starting @ 6
  Result = 5
  Looking for AB, starting @ 5
  Result = 0
Specifying a start position and limit in the character scan
program Project1;
{$APPTYPE CONSOLE}

var
  strA   : String;
  result : Integer;

begin
  strA := 'ABCDEABCDE';

  Console.WriteLine('Guide= 0123456789');
  Console.WriteLine('strA = ' + strA);

  result := strA.LastIndexOf('AB', 9, 4);
  Console.WriteLine('Looking for AB, starting @ 9 back for count of 4');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.LastIndexOf('AB', 8, 4);
  Console.WriteLine('Looking for AB, starting @ 9 back for count of 4');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.LastIndexOf('AB', 5, 6);
  Console.WriteLine('Looking for AB, starting @ 5 back for count of 6');
  Console.WriteLine('Result = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  Guide= 0123456789
  strA = ABCDEABCDE
  Looking for AB, starting @ 9 back for count of 4
  Result = -1
  Looking for AB, starting @ 8 back for count of 4
  Result = 5
  Looking for AB, starting @ 5 back for count of 6
  Result = 0
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author