问题
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