在Perl中,可以使用index函數來查找一個子字符串在另一個字符串中的位置。
例如,下面是一個簡單的示例來查找子字符串"world"在字符串"hello world"中的位置:
my $str = "hello world";
my $substr = "world";
my $pos = index($str, $substr);
if ($pos != -1) {
print "The substring '$substr' was found at position $pos\n";
} else {
print "The substring '$substr' was not found\n";
}
上述代碼將輸出:“The substring ‘world’ was found at position 6”,表示子字符串"world"在字符串"hello world"中的位置是第6個字符(從0開始計數)。