Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 (5.8.8.822/Windows XP or 5.10.0.1004/Suse Linux 11.0) with the same "false" result.
(I think I'm wrong, but i can't find where).

for example:
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
Didier

In reply to function length() in UTF-8 context by didess

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found