From: Heesob Park Date: 2008-09-16T17:23:52+09:00 Subject: Re: get a portion of a utf8 encoded string Hi, 2008/9/16 Marco : > hi all. > > ruby 1.8.5 (2006-08-25) [i386-linux] > >>> puts 'hällo' > hällo > => nil >>> puts 'hällo'[0, 2] > h▒ > => nil >>> puts 'hällo'[0, 3] > hä > => nil >>> require 'iconv' > => true >>> puts 'hällo'[0, 3] > hä > => nil >>> puts 'hällo'[0, 2] > h▒ > => nil >>> > > > The ä character is probably 2 bytes long, so puts 'hällo'[1, 1] returns only > a "pice" of the ä character. Is there a way to make this work? > 'hällo'[0, 2] should return -> hä > and > 'hällo'[0, 3] should return -> häl > > thanks > > You can do it using regular expression : 'hällo'.split(//u)[0, 2].to_s -> hä Regards, Park Heesob