Perl Newbies

  1. Perl
  2. Functions
  3. here

rindex function - Search for a substring from the end of a string

rindex function searches for a substring from the end of a string.

my $pos = rindex($target, $search);

rindex function searches for a substring from the end of a string, while index function searches for a substring from the beginning of a string. First argument is a string to be searched. Second argument is a substring to be searched for. Third argument is starting position for searching. If omitted, rindex function searches a substring from the end of a string. If a substring is found, the position is returned. If not found, -1 is returned.

rindex function programming example

rindex function programming example

# Search for a substring from the end of a string
my $message = "love love love.";
my $word_love_pos_last = rindex($message, 'love');
Menu