How to parse a String like "14px" to get Int value 14 in Swift

Dmytryk Skorokhod

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

Nirav D

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))))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to i save style="position: relative; left: 77px; top: -14px;" in php variable

font-size:14px do not works

why some websites change default font size to 14px

Why does svg alignment change when parent div is < 14px in size?

changing fontsize from `10px` to `14px` to `18px` with a single shortcut

Bulk Resize SVG Canvas from 14px to 16px

After the migration of antd from 2.0 -> 3.0 the default font size got increased from 12px -> 14px , is there a way to customize the antd fontsize?

How to parse a string and get the value after "=" character

In Swift, how to insert formatted Int value into String?

Unable to parse int the string value

How to parse string in swift

How to get a String value from an EditText Int?

How can I parse String to int with the default value?

Swift how to use enum to get string value

How to get a String array of years like monthSymbols in Swift?

How to parse a string into a nullable int

How to get int value of Enum by providing string value in varibale

How can I parse this string to get a specific value?

Get Int from String in Swift

How do I get the variable with the highest Int and retrieve a String Swift

Get value of INT for a "STRING" variable

How to parse "{}" empty value (into String)

How to Get Int or String Value from Textview setted by placeholder

How to get an element's int value from a String matrix?

In Swift, how to convert String to Int for base 2 though base 36 like Long.parseLong in Java?

How to check if a String is an Int in Swift?

How to convert a String to an Int in Swift?

How to get "string" attribute value of CATextLayer in swift 4?

How to get the String value of selected row in a UIPickerView in Swift 3?