Jig*_*hel 5 ruby string unicode dynamic
我有一个需要使用插值动态创建unicode字符串的要求。例如,请参见以下在irb中试用的代码
2.1.2 :016 > hex = 0x0905
=> 2309
2.1.2 :017 > b = "\u#{hex}"
SyntaxError: (irb):17: invalid Unicode escape
b = "\u#{hex}"
Run Code Online (Sandbox Code Playgroud)
十六进制代码0x0905对应于DEVANAGARI字母A的独立元音的unicode。
我无法弄清楚如何达到期望的结果。
您可以将编码传递给Integer#chr:
hex = 0x0905\nhex.chr(\'UTF-8\') #=> "\xe0\xa4\x85"\nRun Code Online (Sandbox Code Playgroud)\n\nEncoding::default_internal如果设置为UTF-8 ,则该参数可以省略:
$ ruby -E UTF-8:UTF-8 -e "p 0x0905.chr"\n"\xe0\xa4\x85"\nRun Code Online (Sandbox Code Playgroud)\n\n您还可以将代码点附加到其他字符串:
\n\n\'\' << hex #=> "\xe0\xa4\x85"\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2530 次 |
| 最近记录: |