How to split string with accented characters in ruby

a 夏天 提交于 2021-01-04 04:57:09

问题


Currently I got :

"mɑ̃ʒe".split('')
# => ["m", "ɑ", "̃", "ʒ", "e"]

I would like to get this result

"mɑ̃ʒe".split('')
# => ["m", "ã", "ʒ", "e"]

回答1:


Use String#each_grapheme_cluster instead. For example:

"mɑ̃ʒe".each_grapheme_cluster.to_a
#=> ["m", "ɑ̃", "ʒ", "e"]


来源:https://stackoverflow.com/questions/64948695/how-to-split-string-with-accented-characters-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!