繁体   English   中英

ruby 字符串转换ascii为unicode

[英]ruby string convert ascii to unicode

我有一个具有 ascii 特殊字符的字符串,我想将它们转换为相应的 unicode 个字符。 例如下面是字符串

A “razor” is a rule of thumb that simplifies decision..  \nWe’re in a post-content age.  In the past,\nhealthier, wealthier life:  • Toxic relationships • Comparisons • Inactivity • Complaints • Instant gratification • Overthinking • Crazy “what if” fears 

期待 output

A "razor" is a rule of thumb that simplifies decision..  \nWe're in a post-content age.  In the past,\nhealthier, wealthier life:  • Toxic relationships • Comparisons • Inactivity • Complaints • Instant gratification • Overthinking • Crazy "what if" fears

我能得到的最好结果是使用unidecode gem。 将上面的字符串转换成这个

"A \"razor\" is a rule of thumb that simplifies decision..\nWe're in a post-content age.  In the past,\nhealthier, wealthier life:  * Toxic relationships * Comparisons * Inactivity * Complaints * Instant gratification * Overthinking * Crazy \"what if\" fears "

该方法的问题是如果字符串是另一种语言,unidecode to_ascii 方法将转换字符。

所以你问的不是 ascii,而是 ASNI,也称为 windows-1252,我建议你看看Windows-1252 wiki,因为它有一个表,表上标有 Unicode 代码点。 从本质上讲,没有简单快捷的方法可以将 ansi 转换为 unicode,并且使用该 wiki 页面中的表格完成的方式与在 unicode 中找到并替换为相同的字形。

关于 ansi、asci 和 unicode 的一件事是前 128 个字符在它们之间完全相同。

就我个人而言,我只是做一个查找表,以及 ruby 似乎如何处理 unicode 字符串使用以下内容:“\u<十六进制代码点>”,您将 <code point in hex> 替换为十六进制值代码点所以说项目符号点:“•”将转换为:“•”如果你需要查找unicode代码点我推荐: unicodeplus.com因为它甚至为你提供了用于每个代码点的转义序列不同的编程语言。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM