#index function to search substring $a = "Ashes, ashes, we all fall down"; print index($a, "she"); #1 print "\n"; print index($a, "ashes"); #7 print "\n"; print index($a, "they"); #-1 print "\n"; @a = qw (oats peas beans); print index (join(" ", @a), "peas"); #5, not useful print "\n"; #indicate start searching position $a = "Ashes, ashes, we all fall down"; print index($a, "she",5); #8, start search at 5 print "\n"; print "============================\n"; #find all occurrences $source = "One fish, two fish, red fish, blue fish."; $start = -1; while (($start = index ($source, "fish", $start)) != -1) { print "Found a fish at $start\n"; $start++; } print "============================\n"; #search backward with rindex $source = "One fish, two fish, red fish, blue fish."; $start = length($source); while (($start = rindex ($source, "fish", $start)) != -1) { print "Found a fish at $start\n"; $start--; } print "============================\n"; #substr (string, offset, length) #extract substring, start at position offset, length chars $a = "I do not like green eggs and ham."; print "length = ", length($a), "\n"; print substr($a, 0, 4); #start at position 0, 4 chars print "\n"; print substr($a, 14, 5); #start at position 14, 5 chars print "\n"; print substr($a, 5, -10); #start at position 5, (33-5-10)+1=18 chars print "\n"; print "============================\n"; #use substr to replace $a = "countrymen, lend me your wallets"; substr($a, 0, 1) = "Romans, C"; #replace first char with... print "$a\n"; substr($a, -7, 7) = "ears."; #replace the last 7 chars print "$a\n";