Get unicode name for characters

If I have a string such as "🚀↑A", how do I transform it to something like "rocket, upwards arrow, latin capital letter a"?

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

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

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"?

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

Terms of Service

Privacy Policy

Cookie Policy