Perl Programming/Keywords/index

Previous: if Keywords Next: INIT

The index keyword

edit

index searches for one string within another without the wildcard-like behavior of a full regular-expression pattern match, and returns the first occurrence index of SUBSTRING in STRING. If also POSITION is passed, the search stars from this position. If POSITION points before the beginning of or after the end of the string, it is regarded as if it points to the beginning or end of it, respectively.

The return value is -1, if SUBSTRING is not found.

Syntax

edit
  index STRING, SUBSTRING, POSITION
  index STRING, SUBSTRING

Examples

edit
 
  $string = "Abrakaddabra";
  $substring = "adda";
  print index($string, $substring);
prints the first occurring index, which is 5:
5


Previous: if Keywords Next: INIT