Using index function to check out if a string contains a sub string : index « String « Perl






Using index function to check out if a string contains a sub string

   


#! /usr/bin/perl -w

use strict;

print "Enter a string:    ";
chomp(my $string = <STDIN>);

print "Enter a substring: ";
chomp(my $substring = <STDIN>);

my $result = index($string, $substring);

if ($result != -1) {
    print "the substring was found at index: $result\n";
} else {
    print "the substring was not found\n";
}

   
    
    
  








Related examples in the same category

1.Demonstration of the index function
2.index function returns the location of the first occurrence of a substring within a string
3.Using index function.
4.Using index to search a line repeatedly.