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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With