From: Martin Duerst Date: 2007-10-31T16:07:47+09:00 Subject: Re: %s and utf8 ? This is a known and difficult problem. For a large character set such as Unicode, it's quite difficult to know the width of every character beforehand. There is Unicode TR 11 (http://www.unicode.org/reports/tr11/, see also proposed update at http://www.unicode.org/reports/tr11/tr11-16.html) that covers part of it, but only part. For many scripts (e.g. Arabic), the idea that each character's width is the same, or can be selected from a couple values, is simply inappropriate. I think that eventually, somebody will come up with a solution for this that at least covers the Western and East Asian cases, but that may take some time. In the meantime, you have to do some of this by hand, e.g. something like def string_to_width(s, w) out = s[0,w] # truncate out + ' '*(width-out.length) end "#{string_to_width(a, 20)} #{string_to_width(b, 20)}" The above only works if all characters have the same width, which may be true in an European context. Regards, Martin. At 05:23 07/10/31, Hadmut Danisch wrote: >Hi, > >I am printing string values with something like > > sprintf("%-20.20s %-20.20s",a,b) > >where a is an utf8 string value. > >Unfortunately the values of b are not aligned in a column, because >the length function for %s counts bytes and not characters. If >a contains any non-ASCII character which is more than a single >character in utf8, the column width is calculated wrong. > > >What would I do? > >regards >Hadmut #-#-# Martin J. Du"rst, Assoc. Professor, Aoyama Gakuin University #-#-# http://www.sw.it.aoyama.ac.jp mailto:duerst@it.aoyama.ac.jp