swift - 将 Unicode 中的结果推迟到 Swift 中的字符串
问题描述
我正在使用此向下代码将我的String值转换为unicodeScalars,例如,如果我将"A"作为输入,我将得到65作为输出,现在我知道65是A的数量,那么我想给65以再次接收A,但它给了e,我确信我将一些主题混合在一起并且还遗漏了一些东西,需要一些帮助来理解为什么这是不同的以及如何使用"\u{???}"来返回我的字符串,谢谢
let string: String = "A"
let UInt32Value: UInt32 = string.unicodeScalars[string.startIndex].value
print(UInt32Value) // 65
let newString = "\u{65}"
print(newString) // e
ps:我知道有使用 unicodescalar 来取回字符串的官方方法,但我对 "\u{?????}" 感兴趣
解决方案
尝试转换如下
let ans = String(UnicodeScalar(UInt8(65)))
print(ans)
我认为问题是,您正在转换 (A) 字符串 -> unicodescalar -> uint
并且必须转换 (65) uint -> unicodescalar -> string 以获得原始值
推荐阅读
- php - 刀片回声问题
- html - 在 fa 图标和文本之间创建空间
- pycharm - Pycharm没有输出显示在控制台中
- javascript - 以递归方式向对象添加新属性失败
- sqlite - 在 Windows 上安装 Perl 模块 DBD::SQLcipher
- bookdown - 在Bookdown中获取输出目录的编程方式?
- python - 如何使用列表格式将更多列连接到一列?
- python - SQLAlchemy:按天对多个表进行分组
- python - 将 spss 文件转换为 pandas 时更改了标题
- c# - 如何解决 IURLHelper 的问题(在 startup.cs 上添加单例不起作用)ASP.NET Core 2.2