Perl Programming/Keywords/index

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Previous: if Keywords Next: INIT

The index keyword[edit | edit source]

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 | edit source]

  index STRING, SUBSTRING, POSITION
  index STRING, SUBSTRING

Examples[edit | edit source]

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


Previous: if Keywords Next: INIT