This function returns the position of the first occurrence of given substring (or pattern) in a string (or text). We can specify start position. By default, it searches from the beginning(i.e. from index zero).
# Searches pat in text from given index
index(text, pat, index)
# Searches pat in text
- text: String in which substring is to be searched.
- pat: Substring to be searched.
- index: Starting index(set by the user or it takes zero by default).
-1 on failure otherwise Position of the matching string.
Position of 'the' in the string: 10
Position of 'Geeks' in the string: -1
Here, in the second example the position is set to ‘3’, i.e. the starting index from where searching is to begin is from 3rd position. Hence, the substring is not found in the String.
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl | String functions (length, lc, uc, index, rindex)
- Perl | split() Function
- Perl | chomp() Function
- Perl | chop() Function
- Perl | rename() Function
- Perl | undef and the defined function
- Perl | cos() Function
- Perl | sin() Function
- Perl | abs() function
- Perl | atan2() Function
- Perl | splice() - The Versatile Function
- Perl | delete() Function
- Perl | defined() Function
- Perl | each() Function
- Perl | int() function
- Perl | join() Function
- Perl | keys() Function
- Perl | lc() Function for Lower Case Conversion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.