我想在Swift中使用十六进制值编写Unicode字符串。我已经阅读了有关字符串和字符的文档,所以我知道可以直接在字符串中使用特殊的Unicode字符,如下所示:
var variableString = "Cat‼🐱" // "Cat" + Double Exclamation + cat emoji
但是我想使用Unicode代码点做到这一点。文档(和这个问题)针对字符显示了它,但是对于如何针对字符串却不太清楚。
(注意:尽管答案对我来说似乎现在很明显,但不久前还不很明显。我在下面回答自己的问题,以此作为学习如何做的方法,并帮助自己了解Unicode术语和Swift字符和字符串的工作方式。)
为Swift 3更新
用于形成一个十六进制迅语法代码点是
\u{n}
其中 n 是一个十六进制数字,最长8位数字。Unicode 标量的有效范围是U + 0到U + D7FF和U + E000到U + 10FFFF(含)。(U + D800到U + DFFF范围用于代理对,它们本身不是标量,而是在UTF-16中用于编码较高值的标量。)
例子:
// The following forms are equivalent. They all produce "C". let char1: Character = "\u{43}" let char2: Character = "\u{0043}" let char3: Character = "\u{00000043}" // Higher value Unicode scalars are done similarly let char4: Character = "\u{203C}" // ‼ (DOUBLE EXCLAMATION MARK character) let char5: Character = "\u{1F431}" // 🐱 (cat emoji) // Characters can be made up of multiple scalars let char7: Character = "\u{65}\u{301}" // é = "e" + accent mark let char8: Character = "\u{65}\u{301}\u{20DD}" // é⃝ = "e" + accent mark + circle
笔记:
字符串由字符组成。有关使用十六进制代码点形成它们的一些方法,请参见以下示例。
var string1 = "\u{0043}\u{0061}\u{0074}\u{203C}\u{1F431}" // Cat‼🐱 // pass an array of characters to a String initializer let catCharacters: [Character] = ["\u{0043}", "\u{0061}", "\u{0074}", "\u{203C}", "\u{1F431}"] // ["C", "a", "t", "‼", "🐱"] let string2 = String(catCharacters) // Cat‼🐱
在运行时,您可以将十六进制或Int值转换为,Character也String可以先将其转换为UnicodeScalar。
Int
Character
String
UnicodeScalar
// hex values let value0: UInt8 = 0x43 // 97 let value1: UInt16 = 0x203C // 22823 let value2: UInt32 = 0x1F431 // 127822 // convert hex to UnicodeScalar let scalar0 = UnicodeScalar(value0) // make sure that UInt16 and UInt32 form valid Unicode values guard let scalar1 = UnicodeScalar(value1), let scalar2 = UnicodeScalar(value2) else { return } // convert to Character let character0 = Character(scalar0) // C let character1 = Character(scalar1) // ‼ let character2 = Character(scalar2) // 🐱 // convert to String let string0 = String(scalar0) // C let string1 = String(scalar1) // ‼ let string2 = String(scalar2) // 🐱 // convert hex array to String let myHexArray = [0x43, 0x61, 0x74, 0x203C, 0x1F431] // an Int array var myString = "" for hexValue in myHexArray { if let scalar = UnicodeScalar(hexValue) { myString.append(Character(scalar)) } } print(myString) // Cat‼🐱