问题的原因是在阿拉伯语中,有一些字符不是单个字符,而是由多个Unicode字符组成的字符群,这些字符即使看起来被删除了,实际上仍然存在。解决这个问题的方法是使用NSString类中的precomposedStringWithCanonicalMapping方法将字符串转换为规范化的格式。
以下是代码示例:
let arabicString = "مرحبا" // "hello" in Arabic
// remove all characters let emptyString = arabicString.replacingOccurrences(of: ".", with: "") // emptyString is still "مرحبا"
// normalize the string let normalizedString = (arabicString as NSString).precomposedStringWithCanonicalMapping // normalizedString is "مرحبا"
// remove all characters from normalized string let emptyNormalizedString = normalizedString.replacingOccurrences(of: ".", with: "") // emptyNormalizedString is ""
现在,即使您从规范化的字符串中删除了所有字符,emptyNormalizedString将是空字符串。
上一篇:ArabicDatestoLongArabicOrdinalFormatwithCorrectedNamesofGregorianandHijriYears(eraformat)”改写为中文的问题。
下一篇:Arabictxt文件和“TypeError:'NoneType'objectisnotsubscriptable”