System.SysUtils.TStringHelper.IndexOfAny

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

function IndexOfAny(const AnyOf: array of Char): Integer; overload;
function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer): Integer; overload;
function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer; Count: Integer): Integer; overload;

Properties

Type Visibility Source Unit Parent
function public System.SysUtils.pas System.SysUtils TStringHelper

Description

Returns an integer indicating the position of the first given character found in the 0-based string.

IndexOfAny uses the following optional parameters:

  • StartIndex specifies the initial offset in this 0-based string where the search starts.
  • Count specifies the maximum length to search starting from StartIndex. It is limited by the string length.

IndexOfAny returns -1 if:

  • The given character is not found.
  • StartIndex specifies a value higher than the string length minus 1 (it is 0-based).
  • Count is equal to or less than 0.

Example

var
  MyString: String;

begin
  MyString := 'This is a string.';
  Writeln(MyString.IndexOfAny(['w'])) 
  Writeln(MyString.IndexOfAny(['w', 's', 'a'], 0)); 
  Writeln(MyString.IndexOfAny(['w', 's', 'a'], 9));
  Writeln(MyString.IndexOfAny(['w', 's', 'a'], 11, 4));
end.

Output:

-1 //  'w' is not present in MyString 
3 // The first given character found is 's' in position 3
10 // Staring at position 9, the first given character found is 's' at position 10
-1 // No given characters are found in the substring 'trin'

See Also