ios - 如何从字符串中删除特殊字符 \u{e2}
问题描述
我有以下代码func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
var phoneNumber: String?
if let callIntent = interaction.intent as? INStartVideoCallIntent {
phoneNumber = callIntent.contacts?.first?.personHandle?.value
} else if let callIntent = interaction.intent as? INStartAudioCallIntent {
phoneNumber = callIntent.contacts?.first?.personHandle?.value
}
电话号码只有 10 位数字。但是当我检查计数时,它显示 12 位数
当我打印字符串时
我努力了
phoneNumber?.trimmingCharacters(in: .whitespacesAndNewlines).count
phoneNumber?.trimmingCharacters(in: .symbols).count
但它仍然返回 12
请帮忙
解决方案
感谢大家的帮助。你真好
我可以使用找到无效字符
phoneNumber?.map{$0.unicodeScalars.allSatisfy{$0.isASCII}}
它返回
Optional<Array<Bool>>
▿ some : 12 elements
- 0 : false
- 1 : true
- 2 : true
- 3 : true
- 4 : true
- 5 : true
- 6 : true
- 7 : true
- 8 : true
- 9 : true
- 10 : false
- 11 : true
我可以用一行来解决这个问题
phoneNumber?.filter{$0.unicodeScalars.allSatisfy{$0.isASCII}}
计数为 10
使用
phoneNumber?.filter{$0.unicodeScalars.allSatisfy{$0.isASCII}}.count
希望对其他人有帮助:)
推荐阅读
- c# - 如何按字符为字符对 TreeView 输出的字典进行排序?
- azure - Azure 逻辑应用获取大于 300MB 的文件内容
- android - 获取与活动相关的错误未在片段中运行
- python - 我想将数据框的值替换为python中的列名。我该怎么做?
- ios - AdMob - 在 isReady 属性更改时获取通知
- javascript - 可以从 url 中省略 ID[] 吗?
- c++ - 从两个不同的类继承的非虚拟基类的访问成员
- jira - Jira - 避免收到有关更改/评论任务的邮件我是受让人
- python-2.7 - Python2 - proc.stdin 未将密码正确传递给 easyrsa
- python - 如何删除包含 alpha 值或整个 dbf 列的 dbf 行