Swift 3: split a string to array by number -


i have string let string = "!101eggs". now, want have array ["!", "101", "e", "g", "g", "s"]. how can this?

sorry it's long

when input is

print("-1-2a000+4-1/000!00005gf101eg14g1s46nj3j4b1j5j23jj212j4b2j41234j01010101g0000z00005g0000".toarraybynumber()) 

result: ["-", "1", "-", "2", "a", "000", "+", "4", "-", "1", "/", "000", "!", "00005", "g", "f", "101", "e", "g", "14", "g", "1", "s", "46", "n", "j", "3", "j", "4", "b", "1", "j", "5", "j", "23", "j", "j", "212", "j", "4", "b", "2", "j", "41234", "j", "01010101", "g", "0000", "z", "00005", "g", "0000"]

    extension int {     func tozerostring() -> string {         return (0 ..< self).reduce("", { (result, zero) -> string in             return result + "0"         })     } }  extension string {      func toarraybynumber() -> [string] {          var array: [string] = []         var num = 0         var zerocount = 0         var zeroend = false          char in self.characters {             if let number = int("\(char)") {                 if zeroend == false && number == 0 {                     zerocount += 1                 } else {                     num = num * 10 + number                     zeroend = true                 }              } else {                 if num != 0 {                     array.append(zerocount.tozerostring() + ("\(num)"))                 } else if zerocount > 0 {                     array.append(zerocount.tozerostring())                 }                   array.append(string(char))                 num = 0                 zerocount = 0                 zeroend = false             }         }          if num != 0 {             array.append(zerocount.tozerostring() + ("\(num)"))         } else if zerocount > 0 {             array.append(zerocount.tozerostring())         }          return array     } } 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -