租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

perl怎么查找字符串位置,perl获取字符串长度

发布时间:2024-06-03 16:44:09

perl怎样查找字符串位置

在Perl中,可使用index函数来查找一个子字符串在另外一个字符串中的位置。

例如,下面是一个简单的示例来查找子字符串"world"在字符串"hello world"中的位置:

my $str = "hello world";
my $substr = "world";

my $pos = index($str, $substr);

if ($pos != ⑴) {
    print "The substring '$substr' was found at position $pos
";
} else {
    print "The substring '$substr' was not found
";
}

上述代码将输出:“The substring ‘world’ was found at position 6”,表示子字符串"world"在字符串"hello world"中的位置是第6个字符(从0开始计数)。