什么是Perl相当于strlen()
?
perldoc -f length length EXPR length Returns the length in characters of the value of EXPR. If EXPR is omitted, returns length of $_. Note that this cannot be used on an entire array or hash to find out how many elements these have. For that, use "scalar @array" and "scalar keys %hash" respectively. Note the characters: if the EXPR is in Unicode, you will get the num- ber of characters, not the number of bytes. To get the length in bytes, use "do { use bytes; length(EXPR) }", see bytes.
虽然'length()'是应该在任何合理的代码中使用的正确答案,但是应该提到阿比盖尔的长度恐怖,如果只是为了Perl传说.
基本上,技巧包括使用catch-all音译操作符的返回值:
print "foo" =~ y===c; # prints 3
y /// c用自己替换所有字符(由于补码选项'c'),并返回替换的字符数(因此,有效地,字符串的长度).
length($string)