http://qs321.pair.com?node_id=1971

vroom has asked for the wisdom of the Perl Monks concerning the following question: (strings)

How do I find the length of a string?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I find the length of a string?
by vroom (His Eminence) on Jan 11, 2000 at 23:28 UTC
    length($string) returns the number of characters in a string.
    $foo = "quux"; $foolen = length $foo; # in this case, 4
Re: How do I find the length of a string?
by vrk (Chaplain) on Jun 04, 2007 at 12:03 UTC

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

Re: How do I find the length of a string?
by gridlock (Novice) on Feb 21, 2004 at 04:31 UTC
    Use the 'length' function

    For a variable $string Its length $stringlength can be found thus:

    $stringlength = length($string);

    Originally posted as a Categorized Answer.

Re: How do I find the length of a string?
by Cazbo (Initiate) on Jun 04, 2007 at 11:39 UTC
    I would just like to add a word of thanks here. I used the syntax of this answer and applied it to the $buffer from a file read, and it worked. Cheers :D

    Originally posted as a Categorized Answer.

Re: How do I find the length of a string?
by heezy (Monk) on Oct 08, 2002 at 16:17 UTC

    I prefer the use of brackets as well in this instance. For any beginers just starting out here's the code using brackets.

    $someText = "Happy happy joy joy"; $someTextLength = length($someText);

    I found it difficult reading posts when I first started out because people were talking casually about stuff that was way over my head.I had no clue what they were on about, so I thought I would simplify this for any newbies

    M

    Originally posted as a Categorized Answer.

Re: How do I find the length of a string?
by Ozyark (Initiate) on Sep 27, 2001 at 01:56 UTC
    its in my opinion easier to read code if you use length($foo) instead of length $foo but thats probably cause the first language i learned was JS

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.