我知道你可以得到一个子字符串substr
但这只是start
位置和length
子字符串。是否有另一个函数利用开始和结束位置,还是我必须$ length = $ end- $ start?有没有办法让perl中的开始和结束位置的子字符串?
my $s = "The black cat climbed the green tree";
my $middle = substr $s, 4, -11; #<-- "black cat climbed the"
我知道你可以得到一个子字符串substr
但这只是start
位置和length
子字符串。是否有另一个函数利用开始和结束位置,还是我必须$ length = $ end- $ start?有没有办法让perl中的开始和结束位置的子字符串?
my $s = "The black cat climbed the green tree";
my $middle = substr $s, 4, -11; #<-- "black cat climbed the"
如果使用负指数,它从尾部计数
join '', (split //, $string)[$start..$end]
但它可能是更好的使用substr
与$end - $start
。
你可以切片的字符串作为字符数组:
在Perl中获取子字符串使用unpack
函数。它的功能比substr
更快。
例:
打印解包( 'A5', '计算器');
print unpack(“x6 A6”,'stackoverflow');
输出:
堆
verflo