Why Strings are not equal in my case?

时光怂恿深爱的人放手 提交于 2019-12-19 10:14:19

问题


I have currencyFormatter that is with se_SV locale.

var currencyFormatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.currencySymbol = ""
    formatter.locale = Locale(identifier: "se_SV")
    formatter.minimumFractionDigits = 2
    formatter.maximumFractionDigits = 2
    formatter.numberStyle = .currencyAccounting
    formatter.isLenient = true
    return formatter
}()

I am converting NSNumber to String.

let firstString = currencyFormatter.string(from: NSNumber(value: 22222222.50)) // "22 222 222,50 "

And than I create String manually that is same as firstString.

let secondString = "22 222 222,50 "

Why when I check if firstString == secondString I receive false?


回答1:


print(Array(firstString!.unicodeScalars))
// ["2", "2", "\u{00A0}", "2", "2", "2", "\u{00A0}", "2", "2", "2", ",", "5", "0", "\u{00A0}"]

print(firstString!.replacingOccurrences(of: "\u{A0}", with: " ") == secondString)
// true

reveals that the number formatter separates the groups with "non-breaking spaces" U+00A0. This prevents the number from being split across lines in a multi-line text.



来源:https://stackoverflow.com/questions/48795085/why-strings-are-not-equal-in-my-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!