I receive from a server API a String like "14px". How to transform this String to Int with value 14 ignoring substring "px"?
Asked
Active
Viewed 152 times
-8
-
1by stripping the "px" or the last two characters away - where is the problem? – luk2302 Dec 29 '16 at 14:24
1 Answers
1
If string always come with just px
at last you can subscript it and ignore the last 2 characters.
let str = "14px"
var numStr = str.substring(to: str.index(name.endIndex, offsetBy: -2))
print(numStr) // "14"
//Now you can convert "14" to Int
print(Int(numStr))
Or
print(Int(String(str.characters.dropLast(2))))

Nirav D
- 71,513
- 12
- 161
- 183