How to convert String to Long in Kotlin?

前端 未结 11 980
旧时难觅i
旧时难觅i 2021-01-31 13:00

So, due to lack of methods like Long.valueOf(String s) I am stuck.

How to convert String to Long in Kotlin?

11条回答
  •  有刺的猬
    2021-01-31 13:44

    Actually, 90% of the time you also need to check the 'long' is valid, so you need:

    "10".toLongOrNull()
    

    There is an 'orNull' equivalent for each 'toLong' of the basic types, and these allow for managing invalid cases with keeping with the Kotlin? idiom.

提交回复
热议问题