Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Learn a Variety of Languages . . . . . . . (was: Re: [tlug] Re: Bourne Shell is the most appropriate scripting language)



On Wed, 17 Jan 2007, Ian MacLean wrote:

really ? - so how do you *easily* get a character count for a string of
utf-8 bytes ?

Well, mostly you don't need to do this. In fact, more often, when doing typical web sites, reporting, and data processing, I need to get the number of character positions a string will take up in a fixed-width font; I'd be interested in seeing how well Python does with that.

However, to answer your question:

    class String
	def utf8_char_count
	    split('').select { |c| c[0] < 128 }.length
	end
    end

Now, for the rest of your program, you can just say String.utf8_char_count.

cjs
--
Curt Sampson       <cjs@example.com>        +81 90 7737 2974


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links