How to split an Int to its individual digits?

后端 未结 8 1237
误落风尘
误落风尘 2020-12-13 06:18

I am trying to split an Int into its individual digits, e.g. 3489 to 3 4 8 9, and then I want to put the digits in an Int array.

I have already tried putting the num

相关标签:
8条回答
  • 2020-12-13 06:54

    You can use this

    extension Int {
    
    func numberOfDigits() -> Int {
        if abs(self) < 10 {
            return 1
        } else {
            return 1 + (self/10).numberOfDigits()
        }
    }
    
    func getDigits() -> [Int] {
        let num = self.numberOfDigits()
        var tempNumber = self
        var digitList = [Int]()
    
        for i in (0..<num).reversed() {
            let divider = Int(pow(CGFloat(10), CGFloat(i)))
            let digit = tempNumber/divider
            digitList.append(digit)
            tempNumber -= digit*divider
        }
        return digitList
    }
    

    }

    0 讨论(0)
  • 2020-12-13 06:58

    You can use this:

    // input -> "123456"
    // output -> [1, 2, 3, 4, 5, 6]
    // Get the string, convert it in an Array(), 
    // use a loop (convert i in Int()), to add it into a container, then return it. Done! 
    
        func getDigitsFromString(for string: String) -> [Int]{
            let stringInt = Array(string)
            var array = [Int]()
            for i in stringInt {
                if let i = Int(String(i)) {
                    array.append(i)
                }
            }
            return array
        }
    
    0 讨论(0)
  • 2020-12-13 07:05

    You can try this:

    var number = "123456"
    var array = number.utf8.map{Int($0)-48}
    

    You can make use of the utf8 property of String to directly access the ASCII value of the characters in the String representation of your number.

    0 讨论(0)
  • 2020-12-13 07:08
    extension Int {
        var digits : [Int] {
            var result = [Int]()
            var remaining = abs(self)
            while remaining > 0 {
                result.insert(remaining % 10, at: 0)
                remaining /= 10
            }
            return result
        }
    }
    
    0 讨论(0)
  • 2020-12-13 07:12

    this code works:

    var number = "123456"
    var array = number.utf8.map{Int(($0 as UInt8)) - 48}
    

    this might be slower:

    var array = number.characters.map{Int(String($0))!}
    

    and if your number is less or equal than Int.max which is 9223372036854775807 here is the fastest variant (and if your number>Int.max you can split your long string that represents your number into 18-digit groups to make this variant work on large strings)

    var array2 = [Int]()
    var i = Int(number)!
    while i > 0 {array2.append(i%10); i/=10}
    array2 = array2.reverse()
    
    0 讨论(0)
  • 2020-12-13 07:15

    A solution without having to convert the int to string....

    Example

    1234%10 = 4 <-
    1234/10 = 123
    123%10 = 3 <-
    123/10 = 12
    12%10 = 2 <- 
    12/10 = 1
    1%10 = 1 <-
    
    var num = 12345
    var arrayInt = [Int]()
    arrayInt.append(num%10)
    while num >= 10 {
      num = num/10
      arrayInt.insert(num%10, at: 0)
    }
    
    0 讨论(0)
提交回复
热议问题