kotlin - 如何在 Kotlin 中将 String 转换为 Long?

标签 kotlin

所以,由于缺乏像 Long.valueOf(String s) 这样的方法,我被卡住了。

如何在 Kotlin 中将 String 转换为 Long?

最佳答案

1. string.toLong()

Parses the string as a [Long] number and returns the result.

@throws NumberFormatException if the string is not a valid representation of a number.

2. string.toLongOrNull()

Parses the string as a [Long] number and returns the result or null if the string is not a valid representation of a number.

3. str.toLong(10)

Parses the string as a [Long] number and returns the result.

@throws NumberFormatException if the string is not a valid representation of a number.

@throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.

public inline fun String.toLong(radix: Int): Long = java.lang.Long.parseLong(this, checkRadix(radix))

4. string.toLongOrNull(10)

Parses the string as a [Long] number and returns the result or null if the string is not a valid representation of a number.

@throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.

public fun String.toLongOrNull(radix: Int): Long? {...}

5. java.lang.Long.valueOf(string)

public static Long valueOf(String s) throws NumberFormatException

关于kotlin - 如何在 Kotlin 中将 String 转换为 Long?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19519468/

相关文章:

android - 将镜像添加到Firebase存储时出错

android - RxJava2 发布

android - 如何更改 Kotlin 属性的名称以匹配 Firebase 数据库上的字段

kotlin - 循环遍历 Kotlin x 次,但不使用索引

gradle - 如何在两个 build.gradle.kts 文件之间共享构建配置?

android - 在 Android Kotlin 中使文本一个接一个地出现(又名打字机效果)

android - 检查本地是否存在 Firestore 文档

android - MVP + RxJava - 将调度器放在 Presenter 或 Interactor 中?

java - 谷歌地图应用程序中未显示地理 Intent 标签

android - 我总是可以在 Kotlin 中将两个条件合并为一行吗?