捕获前8个字符Perl

Ale*_*vic 5 perl

我怎么能取一个字符串的8个字符,例如:

如果我喜欢这个:

my $word ="take first 8 characters";
Run Code Online (Sandbox Code Playgroud)

我怎么打印这个:take fir

ike*_*ami 22

使用substr.

print substr($word, 0, 8);
Run Code Online (Sandbox Code Playgroud)


Dan*_*Dan 7

使用内置substr功能.substr($string, $start, $length)


Dav*_*dRR 6

来自perlfaq4如何访问或更改字符串的 N 个字符?:

您可以使用 substr() 访问字符串的第一个字符。例如,要获取第一个字符,请从位置 0 开始并获取长度为 1 的字符串。

my $string = "Just another Perl Hacker";
my $first_char = substr( $string, 0, 1 ); # 'J'
Run Code Online (Sandbox Code Playgroud)

(有关更改字符串部分的指导,请参阅本文的其余部分。)