Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you turn an Array of codepoints (Int32) to a string?

In Crystal, a String can be turned into an Array(Int32) of codepoints:

"abc".codepoints # [97,98,99] 

Is there a way to turn the Array back into a String?

like image 355
dgo.a Avatar asked Sep 17 '25 21:09

dgo.a


1 Answers

  str     = "aа€æ∡"
  arr     = str.codepoints              # Array(Int32)
  new_str = arr.map { |x| x.chr }.join

  puts str
  puts new_str
  puts(str == new_str)

The .chr instance method can be used to get the Unicode codepoint of an Int. You then .join the individual chars into a new String.

like image 129
dgo.a Avatar answered Sep 19 '25 14:09

dgo.a