【发布时间】:2020-08-11 13:30:41
【问题描述】:
我正在努力从 Swift 中的字符串中提取数字。我正在执行以下操作:
let expression: String = "abc123aa223bb45"
for (indx, ch) in expression.enumerated() {
if ch.isLetter { continue }
else if ch.isNumber {
var val: Int = 0
while(indx < expression.count && ch.isNumber){
val = val * 10 + (ch.asciiValue - 48)
//how can I proceed and increment both indx and ch ...
}
// here I'm going to do smthing with every extracted number
}
}
有人可以帮我解决我遇到的这个问题吗?
我也使用扩展程序
extension Character {
var asciiValue: Int {
get {
let s = String(self).unicodeScalars
return Int(s[s.startIndex].value)
}
}
}
正如您在发布的代码片段中看到的那样
【问题讨论】:
标签: swift string loops while-loop numbers