Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

How do I find the length of a string?

( #1971=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Jan 11, 2000 at 23:27 UTC
Q&A  > strings

Answer: How do I find the length of a string?
contributed by vroom

length($string) returns the number of characters in a string.

$foo = "quux"; $foolen = length $foo; # in this case, 4
Answer: How do I find the length of a string?
contributed by w-ber

Since Perl 5.6, Perl has had Unicode support, which manifests itself as UTF-8 encoded strings. If you want to find out how many characters a string has, simply use length($string). It just works.

However, if, for some reason, you need to find out how many bytes a string uses (which may be different from how many UTF-8 characters the string has):

use bytes; my $len = bytes::length($string);

Unless you know what you are doing, better steer clear of the latter. Use the former. (See perlunicode for more information.)


  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (8)
    As of 2014-12-25 05:21 GMT
    Find Nodes?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?

      Results (159 votes), past polls