From: Rob Biedenharn Date: 2008-08-20T04:37:28+09:00 Subject: Re: Cut a string if length > n On Aug 19, 2008, at 1:42 PM, Pål Bergström wrote: > Pål Bergström wrote: > > Don't say I've run into a classic encoding issue. :-( > > Does Ruby have a problem with utf8 support? > -- > Posted via http://www.ruby-forum.com/. > I did this for a project (way back in Rails 1.1.6) $KCODE = 'UTF8' require 'jcode' class String def first(limit = 1) self.match(%r{^(.{0,#{limit}})})[1] end end You'd still have to assign this, but: irb> name = "P\303\245l Bergstr\303\266m" => "P\303\245l Bergstr\303\266m" irb> puts name Pål Bergström => nil irb> puts name.first(2) På => nil irb> puts name.first(3) Pål => nil irb> puts name.first(12) Pål Bergströ => nil irb> puts name[0...12] Pål Bergstr => nil irb> puts name[0...2] P? => nil irb> p name[0...2] "P\303" => nil I hope that helps you. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com