Home  |  Delphi .net Home  |  System.String  |  IndexOf Method
IndexOf  
Method  
Finds the first index position of a string within a string
String Class
System NameSpace
CF1.  Function IndexOf ( Needle : Char; ) : Integer ;
CF2.  Function IndexOf ( Needle:CharNeedle : Char; Start : Integer; ) : Integer ;
CF3.  Function IndexOf ( Needle:CharNeedle : Char; Start : Integer; Count : Integer; ) : Integer ;
CF4.  Function IndexOf ( Needle : String; ) : Integer ;
CF5.  Function IndexOf ( Needle:StringNeedle : String; Start : Integer; ) : Integer ;
CF6.  Function IndexOf ( Needle:StringNeedle : String; Start : Integer; Count : Integer; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns the 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 beginning and continues until either the Needle is found, or the string is exhausted.
 
The scan can be forced to commence from the Start character, 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 := '123456789';

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

  result := strA.IndexOf('45');
  Console.WriteLine('Index of  45 = ' + result.ToString);

  result := strA.IndexOf('54');
  Console.WriteLine('Index of  54 = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  strA = 123456789
  Index of  45 = 3
  Index of  54 = -1
Specifying a start position
program Project1;
{$APPTYPE CONSOLE}

var
  strA   : String;
  result : Integer;

begin
  strA := 'ABCABCABC';

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

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

var
  strA   : String;
  result : Integer;

begin
  strA := 'ABCABCABC';

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

  result := strA.IndexOf('ABC', 5, 3);
  Console.WriteLine('Looking for ABC, starting @ 5 for count of 3');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.IndexOf('ABC', 5, 4);
  Console.WriteLine('Looking for ABC, starting @ 5 for count of 4');
  Console.WriteLine('Result = ' + result.ToString);

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