gpt4 book ai didi

iphone - NSString 的 isEqualToString 的奇怪结果

转载 作者:行者123 更新时间:2023-12-03 19:34:58 26 4
gpt4 key购买 nike

我不断遇到(罕见)NSStrings 不返回相等的情况,尽管它们在控制台中打印时看起来完全相同。例如:

[string1 isEqualToString:string2]
> comparing 'angelo debarre' to 'angelo debarre'

一直返回NO。我尝试过使用 NSString 的 stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet] 但仍然没有运气。我还尝试使用比较器,例如 compare:localizedCaseInsensitiveCompare: 但我一直得到相同的结果。知道我可能会忽略什么吗?

最佳答案

尝试使用 NSString Unicode 规范化函数之一分解字符串,例如 - (NSString *)decomposedStringWithCompatibilityMapping。兼容性映射(与规范映射)应该是您想要的,因为它将视觉上相似的字符序列分解为相同的基本组件。不过,我还没有明确查找不间断空格是否标准化为空格。

编辑:兼容性映射规范化有两种类型:分解和组合。组合版本是 - (NSString *)precomposedStringWithCompatibilityMapping。不确定哪一个总体上效果更好,但是 Unicode Standard Annex #15 (Unicode Normalization Forms) KC/KD 表格如下:

Normalization Form KC additionally folds the differences between compatibility-equivalent characters that are inappropriately distinguished in many circumstances. For example, the halfwidth and fullwidth katakana characters will normalize to the same strings, as will Roman numerals and their letter equivalents.

Normalization Forms KC and KD must not be blindly applied to arbitrary text. Because they erase many formatting distinctions, they will prevent round-trip conversion to and from many legacy character sets, and unless supplanted by formatting markup, they may remove distinctions that are important to the semantics of the text. It is best to think of these Normalization Forms as being like uppercase or lowercase mappings: useful in certain contexts for identifying core meanings, but also performing modifications to the text that may not always be appropriate. They can be applied more freely to domains with restricted character sets.

关于iphone - NSString 的 isEqualToString 的奇怪结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6824906/

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