Ruby Chinese character substring difficulties
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
精彩评论