Thursday, October 20, 2022

Substring in perl

 

$foo = "Hello world";

@arr = split("",$foo);
print $arr[6]; # returns w
print "\n";

print substr($foo, 6, 5); # returns 'world'
 

Finding position of a substring

In Perl, we can use the index method to get the first position/occurrence of a substring

 

 in another string. If the substring does not exist, the index returns -1.

 

 

$str = "The occurence of hay is hay at position:";

print $str . index($str, "hay")."\n";
 

No comments:

Post a Comment