Finding Strings in Strings: Index and Substr
#!/usr/bin/perl -w
#################### Display of index function and ######################
#################### its use to find all occurrences #######################
#################### of a substring in a string. ######################
$string = "Mississippi is in history.";
$offset = 0;
while (($offset = index($string, "is", $offset)) != -1)
{
print "\"is\" encountered at offset: $offset\n";
$offset += length("is");
}
####################### Similar use of rindex. ########################
$string = "Mississippi is in history.";
$offset = length($string);
while (($offset = rindex($string, "is", $offset)) != -1)
{
print "\n\"is\" encountered at offset: $offset";
$offset -= length("is");
}
####################### Substr function demo #########################
$string = "Mississippi is in history."; ##### Create triangle #1
$offset = 0;
$length = 1;
while($substr = substr($string, $offset, $length), $length <= length($string))
{
print "$substr\n";
$length++;
}
$string = "Mississippi is in history."; ##### Create triangle #2
$offset = -1;
$length = 1;
while($substr = substr($string,$offset,$length), $length <= length($string))
{
print "$substr\n";
$offset--;
$length++;
}
$string = "Mississippi is in history."; ##### Create triangle #3
$offset = -1;
while($substr = substr($string,$offset), -$offset <= length($string))
{
print "$substr\n";
$offset--;
}
$string = "Mississippi is in history."; ##### Create triangle #4
$offset = 0;
$length = -1;
print "$substr\n" while(length($substr = substr($string,$offset,$length--))
!= 0);
######################## Substr on Left Side of = #######################
print "\n";
$string = "Mississippi is in history."; # Replace "Miss" with "Mister"
substr($string, 0, 4) = "Mister";
print "$string\n";
print "\n";
$string = "Mississippi is in history.";
substr($string, 0, 4) = "??"; # Replace "Miss" with "??"
print "$string\n";
print "\n";
$string = "Mississippi is in history.";
substr($string, -4, 4) = "??"; # Replace "ory." with "??"
print "$string\n";
print "\n";
$string = "Mississippi is in history.";
substr($string, 0, 0) = "I miss "; # Prepend "I miss "
print "$string\n";
print "\n";
$string = "Mississippi is in history.";
substr($string, length($string) ) = "I miss "; # Append "I miss "
print "$string\n";
####################### Program Output Below #########################
"is" encountered at offset: 1
"is" encountered at offset: 4
"is" encountered at offset: 12
"is" encountered at offset: 19
"is" encountered at offset: 19
"is" encountered at offset: 12
"is" encountered at offset: 4
"is" encountered at offset: 1
M ########## Triangle #1
Mi
Mis
Miss
Missi
Missis
Mississ
Mississi
Mississip
Mississipp
Mississippi
Mississippi
Mississippi i
Mississippi is
Mississippi is
Mississippi is i
Mississippi is in
Mississippi is in
Mississippi is in h
Mississippi is in hi
Mississippi is in his
Mississippi is in hist
Mississippi is in histo
Mississippi is in histor
Mississippi is in history
Mississippi is in history.
. ############ Triangle #2
y.
ry.
ory.
tory.
story.
istory.
history.
history.
n history.
in history.
in history.
s in history.
is in history.
is in history.
i is in history.
pi is in history.
ppi is in history.
ippi is in history.
sippi is in history.
ssippi is in history.
issippi is in history.
sissippi is in history.
ssissippi is in history.
ississippi is in history.
Mississippi is in history.
. ############ Triangle #3
y.
ry.
ory.
tory.
story.
istory.
history.
history.
n history.
in history.
in history.
s in history.
is in history.
is in history.
i is in history.
pi is in history.
ppi is in history.
ippi is in history.
sippi is in history.
ssippi is in history.
issippi is in history.
sissippi is in history.
ssissippi is in history.
ississippi is in history.
Mississippi is in history.
Mississippi is in history ########## Triangle #4
Mississippi is in histor
Mississippi is in histo
Mississippi is in hist
Mississippi is in his
Mississippi is in hi
Mississippi is in h
Mississippi is in
Mississippi is in
Mississippi is i
Mississippi is
Mississippi is
Mississippi i
Mississippi
Mississippi
Mississipp
Mississip
Mississi
Mississ
Missis
Missi
Miss
Mis
Mi
M
Misterissippi is in history.
??issippi is in history.
Mississippi is in hist??
I miss Mississippi is in history.
Mississippi is in history.I miss