开发者

Ruby Chinese character substring difficulties

开发者 https://www.devze.com 2023-02-12 09:56 出处:网络
For my rails site, there\'s some UI with only enough space to display the first 5 characters of a user\'s name. So I\'m truncating the string to display as follows:

For my rails site, there's some UI with only enough space to display the first 5 characters of a user's name. So I'm truncating the string to display as follows:

@user.name[0..4]

It works if the name is in English. But if @user.name contains Chinese (multibyte) characters, two problems arise. The first problem is that [0..4] only gives me 2 characters, not 5. The second problem is that sometimes the last character gets cut in half and garbage shows up on screen.

I was wondering if there's som开发者_JS百科e relatively clean way to handle substring-ing multibyte characters in ruby?


Here's an excellent article about Ruby 1.8 and multibyte support (or, rather, the lack of it).

Based on what's there, you can try doing something like:

# this should get you first 4 characters of the string:
your_chinese_string.scan(/./mu)[0,4].join
0

精彩评论

暂无评论...
验证码 换一张
取 消