string 如何在 Perl 中获取字符串的长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/223393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:18:32  来源:igfitidea点击:

How do I get the length of a string in Perl?

perlstring

提问by Kip

What is the Perl equivalent of strlen()?

Perl 相当于strlen()什么?

回答by Paul Tomblin

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.

回答by Yanick

Although 'length()' is the correct answer that should be used in any sane code, Abigail's length horrorshould be mentioned, if only for the sake of Perl lore.

尽管 'length()' 是应该在任何理智的代码中使用的正确答案,但应该提到Abigail 的长度恐怖,即使只是为了 Perl 知识。

Basically, the trick consists of using the return value of the catch-all transliteration operator:

基本上,这个技巧包括使用 catch-all 音译运算符的返回值:

print "foo" =~ y===c;   # prints 3

y///c replaces all characters with themselves (thanks to the complement option 'c'), and returns the number of character replaced (so, effectively, the length of the string).

y///c 用自己替换所有字符(感谢补码选项 'c'),并返回被替换的字符数(因此,实际上是字符串的长度)。

回答by JDrago

length($string)

回答by RANA DINESH

The length()function:

length()函数:

$string ='String Name';
$size=length($string);

回答by pslessard

You shouldn't use this, since length($string) is simpler and more readable, but I came across some of these while looking through code and was confused, so in case anyone else does, these also get the length of a string:

您不应该使用它,因为 length($string) 更简单且更具可读性,但是我在查看代码时遇到了其中的一些并且感到困惑,所以万一其他人这样做,这些也会得到字符串的长度:

my $length = map $_, $str =~ /(.)/gs;
my $length = () = $str =~ /(.)/gs;
my $length = split '', $str;

The first two work by using the global flag to match each character in the string, then using the returned list of matches in a scalar context to get the number of characters. The third works similarly by splitting on each character instead of regex-matching and using the resulting list in scalar context

前两个通过使用全局标志来匹配字符串中的每个字符,然后在标量上下文中使用返回的匹配列表来获取字符数。第三个通过拆分每个字符而不是正则表达式匹配并在标量上下文中使用结果列表来类似地工作

回答by sqldoug

Not an answer but an observation: I'm running v5.30.2 built for darwin-2level, and I've noticed that if I don't chompmy input first (as with stdin), then calculate the length, the lengthof a single character is not 1, but 2.

不是答案而是观察:我正在运行为 darwin-2level 构建的 v5.30.2,我注意到如果我不chomp先输入(与标准输入一样),然后计算长度,length单个字符的不是 1,而是 2。

Remember those newlines and other hidden characters that could be a factor.

记住那些可能是一个因素的换行符和其他隐藏字符。

And I would love to find Abigail's post, but alas it seems to have been consumed by the ether.

我很想找到阿比盖尔的帖子,但可惜它似乎已经被以太消耗了。