Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Limiting a UTF-8 string by byte-length

This RabbitMQ page states:

Queue names may be up to 255 bytes of UTF-8 characters.

In ruby (1.9.3), how would I truncate a UTF-8 string by byte-count without breaking in the middle of a character? The resulting string should be the longest possible valid UTF-8 string that fits in the byte limit.

like image 337
Kelvin Avatar asked Sep 21 '12 18:09

Kelvin


1 Answers

For Rails >= 3.0 you have ActiveSupport::Multibyte::Chars limit method.

From API docs:

- (Object) limit(limit) 

Limit the byte size of the string to a number of bytes without breaking characters. Usable when the storage for a string is limited for some reason.

Example:

'こんにちは'.mb_chars.limit(7).to_s # => "こん"
like image 87
jogaco Avatar answered Sep 21 '22 17:09

jogaco