string - 如何将代码点数组 (Int32) 转换为字符串?

标签 string crystal-lang codepoint

在 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/

相关文章:

crystal-lang - 斐波那契问题导致算术溢出

使用 Crystal 的 Ruby gem native 扩展?

class - 如何通过 "actual"类找到实例变量?

c - printf() 函数如何处理使用全局缓冲区的函数返回的字符串?

Python - collections.OrderedDict() 未正确排序字典

java - 如果我使用 Java 8 的 String.codePoints 获取一个 int codePoints 数组,数组的长度是字符数吗?

python - 如何在 python 中轻松地以 unicode 编码一长串十六进制值

java - String.codePointAt 到底是做什么的?

java - 将引用值添加到 Enum

Java命令存储