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

后端 未结 6 1700
清歌不尽
清歌不尽 2020-12-29 18:14

What is the Perl equivalent of strlen()?

6条回答
  •  不思量自难忘°
    2020-12-29 18:39

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

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

    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).

提交回复
热议问题