Given an array of strings that represent unicode code points:
let arr : [String] = ["0023", "FE0F", "20E3"]
How can I dynamically convert this into a swift string? Statically, I have found I can write:
let str = "\u{0023}\u{FE0F}\u{20E3}"
However, I would like to do this dynamically as each array will represent some sequence of code points. In the above example, the output would be #️⃣
You can convert each of your hexa string to
UInt32
, initialise anUnicode.Scalar
for each element and create aString
UnicodeScalarView
from it:Which can be also be written as a one liner:
edit/update:
If all your strings can be represented by UInt16 values you can also use String initializer
init(utf16CodeUnits: UnsafePointer<unichar>, count: Int)
as shown by @MartinR here: