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
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
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.
 
end.
Hide 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