我有以下代码:

let kmInput: Int = (addKmInput.text!.replacingOccurrences(of: ".", with: "") as NSString).integerValue

我可以另外替换逗号的出现吗?或者是唯一的解决方法:
let kmInput: Int = (addKmInput.text!.replacingOccurrences(of: ".", with: "").replacingOccurrences(of: ",", with: "")

最佳答案

Yo可以使用支持正则表达式的replacingOccurrences,在这种情况下[,.]

let result = string.replacingOccurrences(of: "[.,]", with: "", options: .regularExpression)

完整的代码是
let kmInput = Int(addKmInput.text!
    .replacingOccurrences(of: "[.,]",
                          with: "",
                          options: .regularExpression))

关于ios - 替换出现的逗号或点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57511365/

10-11 00:12