Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Parsing UTF-8 encoded strings in Ruby (See related posts)

Instead of using $KCODE = 'UTF8' together with require 'jcode' you can use the /u regex parameter
to parse UTF-8 strings containing multibyte characters.

A Latin1 <-> UTF-8 conversion hack btw can be found here:
http://rubyforge.org/pipermail/fxruby-users/2005-September/000480.html

For comparison just drop the u option!


string = "abc\303\244"  #  \303\244 stands for รค

puts string.scan(/./u).size

puts string.split(//u).reverse.join

puts string.gsub(/.$/u, '')

regex = Regexp.new(/..../u)
md = regex.match(string)
puts md[0].inspect



You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts