swift - 如何以特定方式从 Swift 中的字符串中提取数字

标签 swift string loops while-loop numbers

我正在努力从 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)
        }
    }
}

正如您在发布的代码片段中看到的那样

最佳答案

我正在尝试编写一个程序来评估带有大括号和运算符的表达式:^,+,-,/,*

但是我遇到了一些问题(我已经用 C++ 和 C# 做过很多次了,但我无法用 swift 来做到这一点。这真的让我很头疼):

import Foundation

extension Character {
    var asciiValue: Int {
        get {
            let s = String(self).unicodeScalars
            return Int(s[s.startIndex].value)
        }
    }
}

precedencegroup PowerPrecedence { higherThan: MultiplicationPrecedence }
infix operator ^^ : PowerPrecedence
func ^^ (radix: Double, power: Int) -> Double {
    return (pow(Double(radix), Double(power)))
}

func calc(a: Double, b: Double, o: Character) -> Double {
    switch o {
    case "+":
        return a + b
    case "-":
        return a - b
    case "/":
        return a / b
    case "*":
        return a * b
    case "^":
        return a ^^ Int(b)
    default:
        return 0
    }
}

func nice(o: Character) -> Int {
    if o == "-" || o == "+" { return 1 }
    if o == "*" || o == "/" { return 2 }
    if o == "^" { return 3 }
    return 2020
}

func evaluate(expression: String) -> Double {
    var vals = [Double]()
    var ops = [Character]()

    for (indx, ch) in expression.enumerated() {
        if ch == " " { continue }
        else if ch == "(" { ops.append(ch) }
        else if ch.isNumber {
            var val: Int = 0
            while(indx < expression.count && ch.isNumber){
val = val * 10 + ch.asciiValue - 48 // gives strange error
                //TODO increment indx and ch
            }
            vals.append(Double(val))
        } else if ch == ")" {
            while !ops.isEmpty && ops.last != "(" {
                let val_2: Double = vals.popLast()!
                let val_1: Double = vals.popLast()!
                let op: Character = ops.popLast()!
                vals.append(calc(a: val_1, b: val_2, o: op))
            }
            if !ops.isEmpty { _ = ops.popLast() } // opening brace
        } else {
            while(!ops.isEmpty && nice(o: ops.last!) >= nice(o: ch)){
                let val_2: Double = vals.popLast()!
                let val_1: Double = vals.popLast()!
                let op: Character = ops.popLast()!
                vals.append(calc(a: val_1, b: val_2, o: op))
            }
            ops.append(ch);
        }
    }
    while !ops.isEmpty {
        let val_2: Double = vals.popLast()!
        let val_1: Double = vals.popLast()!
        let op: Character = ops.popLast()!
        vals.append(calc(a: val_1, b: val_2, o: op))
    }
    return vals.last!
}

let exp: String = "2^3+(2+3)"
print(evaluate(expression: exp))

关于swift - 如何以特定方式从 Swift 中的字符串中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61468281/

相关文章:

regex - 在 bash 中拆分字符串

javascript - 如何更好地迭代包含大量未定义项的大型数组

python - 在 sql 查询 python 中使用时,格式选项不起作用

php - 如何根据数据库结果将多个 PHP 复选框设置为选中状态?

java - 如何重新启动程序

swift - 用于安排通知的唯一标识符,Swift 3 iOS 10

ios - 如何在某个 CGPoint 创建 UIView?

ios - 具有多个 CollectionViewLayout 的单个 CollectionView。为什么布局困惑?

ios - 更新父类(super class)的 TableView

c++ - [性能]--- string::operator+= vs. vector<char> push_back