gpt4 book ai didi

ios - 如何在 Swift 中打印 Unicode 字符

转载 作者:行者123 更新时间:2023-11-30 11:12:00 28 4
gpt4 key购买 nike

我想打印一些特殊符号的 Unicode 字符,如 `,',@,$,所以请告诉我是否有任何方法可以将字符串转换为 unicode 字符,如\u00e2...

谢谢

最佳答案

您可以获得字符串 unicodeScalars 值,转换为十六进制并用前导零填充它:

extension String {
var unicodes: [String] {
return unicodeScalars.map{ String($0.value, radix: 16) }
}
func paddingToLeft(maxLength: Int) -> String {
return repeatElement("0", count: max(0,maxLength-count)) + self
}
}
<小时/>
let str =  "\u{00e2}"
let hexa = str.unicodes.first!
print(hexa.paddingToLeft(maxLength: 4)) // "00e2\n"

关于ios - 如何在 Swift 中打印 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52091673/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com