Substring.IndexOf Method

Overload List

#SignatureDescription
1function IndexOf(const C: Char; const StartIndex: Integer = 0): Integer; overload;
2function IndexOf(const C: Char; const StartIndex, Count: Integer): Integer; overload;
3function IndexOf(const S: Substring; const StartIndex: Integer = 0): Integer; overload;The 0-based index of the first occurrence of S at or after StartIndex.
4function IndexOf(const S: string; const StartIndex: Integer = 0): Integer; overload;
5function IndexOf(const S: string; const StartIndex, Count: Integer): Integer; overload;The first occurrence of S lying wholly inside the Count characters from StartIndex.

Overload 1: function IndexOf(const C: Char; const StartIndex: Integer = 0): Integer; overload;

#NameTypeDescription
1CChar
2StartIndexInteger

Returns: Integer

Overload 2: function IndexOf(const C: Char; const StartIndex, Count: Integer): Integer; overload;

#NameTypeDescription
1CChar
2StartIndexInteger
3CountInteger

Returns: Integer

Overload 3: function IndexOf(const S: Substring; const StartIndex: Integer = 0): Integer; overload;

The 0-based index of the first occurrence of S at or after StartIndex.

#NameDescription
1S

A text of every kind, or a Char.

2StartIndex

0-based, from 0 to Length; outside it ESubstringRangeError is raised.

Returns: Integer - The index, or -1 when there is none; an empty S is never found.

Examples

Searching a text and a character.

uses Substr;

procedure Example;
var s: Substring;
begin
    s := Substring('banana');
    Assert(s.IndexOf('an') = 1);
    Assert(s.IndexOf('an', 2) = 3);
    Assert(s.IndexOf(Char('n')) = 2);
    Assert(s.IndexOf('x') = -1);
end;
See Also: Substring.Pos, Substring.Pos

Overload 4: function IndexOf(const S: string; const StartIndex: Integer = 0): Integer; overload;

#NameTypeDescription
1Sstring
2StartIndexInteger

Returns: Integer

Overload 5: function IndexOf(const S: string; const StartIndex, Count: Integer): Integer; overload;

The first occurrence of S lying wholly inside the Count characters from StartIndex.

#NameTypeDescription
1Sstring
2StartIndexInteger
3CountInteger

Returns: Integer - The 0-based index, or -1.

Remarks:

Only that range is searched; a range outside the text raises ESubstringRangeError.

Examples

A match must lie inside the range.

uses Substr;

procedure Example;
var s: Substring;
begin
    s := Substring('banana');
    Assert(s.IndexOf('an', 2, 3) = 3);
    Assert(s.IndexOf('an', 2, 2) = -1);
end;