logo       

Re: [jruby-user] Iterating over characters in a String ?: msg#00145

lang.jruby.user

Subject: Re: [jruby-user] Iterating over characters in a String ?

Leonel Gayard wrote:
Ok, I'll take a look at that.

So, in the meantime, for iterating over characters, is there a better
solution than this ?

def each_char(str)
0...str.length do |i| puts str[i] end
end

This is quite important. I'm writing my app in Brazilian Portuguese,
which with all its accents ("Olá, como vai você ?") does not fit in
the ASCII subset of UTF-8, and I really need Unicode.

If you know what character set you are using, you can use the unpack method. For example, if the bytes represent UTF-8 characters, then

str.unpack('U*')

will return an array of unsigned integers. If you are using ISO-8859-1, then try

str.unpack('C*')

which returns an array of Fixnums.

// Gregory

---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise