Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有类似的代码
text[from..to] = text_insertion
问题是from并且to将 UTF8 编码的字符计算为一个字符,而上面的代码在 Ruby 1.8.7 中按字节计算
from
to
我必须使用 Ruby 1.8.7
我可以使用哪个 gem 来使我的代码正常工作?我也可以重写它以使用String#substr或类似的东西,但我还没有找到任何库,它给了我尊重 UTF8 编码的方法。
String#substr
您可以使用 ActiveSupport 的String#mb_chars方法。它让你写这样的东西:
String#mb_chars
text.mb_chars[from..to] = text_insertion
自 2.2 起,它在所有版本的 ActiveSupport 中都可用。