-7

I receive from a server API a String like "14px". How to transform this String to Int with value 14 ignoring substring "px"?

1 Answers1

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
  • 65,660
  • 11
  • 144
  • 172