为什么字符串在我的情况下不相等?

时间:2018-02-14 19:48:34

标签: swift string number-formatting

我的currencyFormatter语言环境为se_SV

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
}()

我正在将NSNumber转换为String

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

而且我手动创建的StringfirstString相同。

let secondString = "22 222 222,50 "

为什么当我检查firstString == secondString时是否收到false

1 个答案:

答案 0 :(得分:4)

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

显示数字格式化程序用“非中断空格”U+00A0分隔这些组。这可以防止数字被分割 多行文字中的行。