|laziness, impatience, and hubris|
function length() in UTF-8 contextby didess (Sexton)
|on Nov 18, 2008 at 23:52 UTC||Need Help??|
didess has asked for the
wisdom of the Perl Monks concerning the following question:
Hi all !
I really need your help for a hint :
I need the length of strings which are UTF-8 coded.
I'm using Perl from Active state (220.127.116.112/Windows XP or 18.104.22.1684/Suse Linux 11.0) with the same "false" result.
(I think I'm wrong, but i can't find where).
length('a') returns 1 : That's right
length('à') returns 2 : That's wrong (I think it should return 1, because it's one CHARACTER long, although 2 BYTES because of UTF-8):
When looking at documentation (perlfunc / length) : I read that :
" Note the characters: if the EXPR is in Unicode, you will get the number of characters, not the number of bytes.
To get the length of the internal string in bytes, use bytes::length(EXPR), see the bytes manpage.
Note that the internal encoding is variable, and the number of bytes usually meaningless.
To get the number of bytes that the string would have when encoded as UTF-8, use length(Encoding::encode_utf8(EXPR))"
It seems to play the opposite role!
Any Idea ?
Thanks in advance