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?
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?
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.
Here's one way:
However, the above fails for multi-byte codepoints: "a€æ∡"