溫馨提示×

perl怎么查找字符串位置

小億
137
2024-06-03 16:44:09
欄目: 編程語言

在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開始計數)。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女