If I have a string such as "🚀↑A", how do I transform it to something like "rocket, upwards arrow, latin capital letter a"?
Jens
2
let str = "🚀↑A"
for c in str {
print(c, c.unicodeScalars.map { $0.properties.name } )
}
or
let str = "🚀↑A"
let transformed = str
.flatMap(\.unicodeScalars)
.compactMap(\.properties.name)
.joined(separator: ", ")
print(transformed) // "ROCKET, UPWARDS ARROW, LATIN CAPITAL LETTER A"
4 Likes
xwu
(Xiaodi Wu)
3
Unicode gives names for Unicode scalars, not characters (extended grapheme literals). You can obtain the Unicode name for a scalar using the property name.
3 Likes
wowbagger
(冀卓疌)
4
name's documentation says:
Some scalars, such as control characters, do not have a value for this property in the Unicode Character Database. For such scalars, this property is nil.
So if I have a string "\t", how do I get something like "character tabulation" or "horizontal tab"?
xwu
(Xiaodi Wu)
5
There are 65 control characters without names in Unicode; you can create a manual list of them for that purpose if you'd like.
or, you can use the Unicode name aliases