简体   繁体   English

替换出现的逗号或点

[英]Replace occurrences of either comma or dot

I have the following code: 我有以下代码:

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

Can I additionally replace occurrences of commas? 我可以额外替换逗号吗? Or is the only way to solve it this way: 或以这种方式解决它的唯一方法是:

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

Yo can use replacingOccurrences that supports regular expressions, in this case [,.] 哟可以使用replacingOccurrences支持正则表达式,在这种情况下[,.]

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

the full code would then be 完整的代码将是

let kmInput = Int(addKmInput.text!
    .replacingOccurrences(of: "[.,]", 
                          with: "", 
                          options: .regularExpression))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM