![]() |
|
Keep It Simple, Stupid | |
PerlMonks |
The “real length" of UTF8 stringsby Anonymous Monk |
on Sep 23, 2008 at 20:04 UTC ( #713297=perlquestion: print w/replies, xml ) | Need Help?? |
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: When printing an UTF8 string with printf("%s"), it can actually be wider than expected due to, by example, the chinese characters that are "2 printed chars" wide on my terminal. My problem occurs when mixing letters with chinese character in a string: I'm unable to guess the actual length of the string to be printed. The following code gives us informations about how the string is encoded inside Perl:
Since I know my characters are 2 characters wide, I can try to guess the "real width" is 8 using the length() function (4 * 2 = 8). But it doesn't work anymore when I enclose my string in brackets: Now I have 6 characters, but I can't guess the "real length" is 10 (6 * 2 != 10), and the bytes length won't help... Does anyone have an idea to measure these strings?
Back to
Seekers of Perl Wisdom
|
|