在 Crystal 中,可以将 String 转换为代码点的 Array(Int32):
"abc".codepoints # [97,98,99]
有没有办法将数组转回字符串?
最佳答案
str = "aа€æ∡"
arr = str.codepoints # Array(Int32)
new_str = arr.map { |x| x.chr }.join
puts str
puts new_str
puts(str == new_str)
.chr instance method可用于获取 Int 的 Unicode 代码点。你然后
.join
单个字符转换成一个新的字符串。
关于string - 如何将代码点数组 (Int32) 转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46289803/