10の乗数混じりの漢数字をアラビア数字に変換する

    // 10の乗数混じりの漢数字を変換する
    func convertNumerialStringToNumberWithString(_ string : String) -> String {
        
        let japaneseNumericalExpChars : [String : String] = [
            "十": "1",
            "百": "2",
            "千": "3",
            "万": "4",
            "億": "8",
            ]
        let japaneseExpChars : Set = Set(japaneseNumericalExpChars.keys)
        
        let convStr : String = string.characters.reversed().reduce("", {
            if $0.0.isEmpty {
                if japaneseExpChars.contains($0.1.description) {
                    return expStr(japaneseNumericalExpChars[$0.1.description]!, isOnlyZero: false)
                } else {
                    return convertCharToStr1To9($0.1)
                }
            }
            
            if japaneseExpChars.contains($0.1.description) {
                return String(Int($0.0)! + Int(expStr(japaneseNumericalExpChars[$0.1.description]!, isOnlyZero: false))!)
            } else {
                return convertCharToStr1To9($0.1) + $0.0.substring(from: $0.0.index(after: $0.0.startIndex))
            }
        })
        
        return convStr
    }

github.com

漢数字をアラビア数字に変換するstring型のextentionを作った

github.com

extension String {
    
    func numeralsToNumber() -> String {
        let japaneseNumericalChars : [String : String] = [
            "〇": "0",
            "一": "1",
            "二": "2",
            "三": "3",
            "四": "4",
            "五": "5",
            "六": "6",
            "七": "7",
            "八": "8",
            "九": "9",
        ]
        let japaneseChars : Set = Set(japaneseNumericalChars.keys)
        
        let strArr : [String] = self.characters.map{
            japaneseChars.contains($0.description) ? japaneseNumericalChars[$0.description]!
                                                   : $0.description
        }
        return strArr.joined()
    }
}

map内の文字を三項演算子で処理することろがグッとこないのであとで直したいような気がする。