Forum: Ruby on Rails first letter of UTF-8 string?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
349194b576c4c44304375d39e7425103?d=identicon&s=25 ianic (Guest)
on 2005-11-12 12:49
(Received via mailing list)
How to get first letter of UTF-8 string?

I try with :
item.name.slice(0..0) -- works ok when first letter is regular ascii
character, don't works for local Croatian characters: "©ÐÈÆ®¹ðèæ¾"

item.name.slice(0..1) -- this works ok for items starting with Croatian
character but returns two letters for other items

Is there slice version (or something like that) which works with
characters not bytes.

Thanks,
Igor.
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 francois.beausoleil (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Hi !

2005/11/9, Igor Anic <ianic@4dva.hr>:
> Is there slice version (or something like that) which works with
> characters not bytes.

Unfortunately, Ruby String objects always work with bytes, not
characters.  You will have to look into the Unicode Gem.
Unfortunately, it requires some kind of local extension to be
installed.  I have never worked with it myself.

Bye,
François
325fce33ff4480be6ed246a6bebc9a00?d=identicon&s=25 usenet (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Igor Anic wrote:
> How to get first letter of UTF-8 string?

$KCODE = 'u'
string.match(/^./).to_s
This topic is locked and can not be replied to.