Home  |  Delphi .net Home  |  system.String  |  IndexOfAny Method
IndexOfAny  
Method  
Finds the first index of any of a set of characters within a string
String Class
system NameSpace
CF1.  Function IndexOfAny ( Values : Array of Char; ) : Integer ;
CF2.  Function IndexOfAny ( Values:Array of CharValues : Array of Char; Start : Integer; ) : Integer ;
CF3.  Function IndexOfAny ( 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 first 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 beginning and continues until either a character 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;
  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.IndexOfAny(strArray);
  Console.WriteLine('Looking for h,o,d');
  Console.WriteLine('Result = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  Guide=           1111111111
         01234567890123456789
  strA = Hello World
  Looking for h,o,d
  Result = 4
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.IndexOfAny(strArray, 0);
  Console.WriteLine('Looking for h,o,d, starting @ 0');
  Console.WriteLine('Result = ' + result.ToString);

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

  result := strA.IndexOfAny(strArray, 8);
  Console.WriteLine('Looking for h,o,d, starting @ 8');
  Console.WriteLine('Result = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  Guide=           1111111111
         01234567890123456789
  strA = Hello World
  Looking for h,o,d, starting @ 0
  Result = 4
  Looking for h,o,d, starting @ 6
  Result = 7
  Looking for h,o,d, starting @ 8
  Result = 10
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.IndexOfAny(strArray, 0, 3);
  Console.WriteLine('Looking for h,o,d, starting @ 0 for a count of 3');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.IndexOfAny(strArray, 6, 3);
  Console.WriteLine('Looking for h,o,d, starting @ 6 for a count of 3');
  Console.WriteLine('Result = ' + result.ToString);

  result := strA.IndexOfAny(strArray, 8, 3);
  Console.WriteLine('Looking for h,o,d, starting @ 8 for a count of 3');
  Console.WriteLine('Result = ' + result.ToString);

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