Paul Battley
7/10/2006 11:09:00 AM
On 10/07/06, xTRiM <rtokarev@gmail.com> wrote:
> is there any way, to detect text encoding?
> For example, is it in utf8, or in win1251, or something else.
You can't detect one-byte-per-character encodings easily (i.e. without
statistical analysis) but you can easily tell if something's UTF-8 or
not:
class String
def is_utf8?
unpack('U*')
return true
rescue
return false
end
end
"foo".is_utf8? #=> true
"foo\303".is_utf8? #=> false
Not the most efficient way, necessarily, but probably the easiest.
Paul.