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

所以,由于缺乏像 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

https://stackoverflow.com/questions/19519468/

相关文章:

kotlin - 什么是 Kotlin 双键 (!!) 运算符?

inheritance - 在 Kotlin 中扩展数据类

dictionary - 如何在 Kotlin 中将列表转换为 map ?

design-patterns - 如何在 Kotlin 中实现 Builder 模式?

hibernate - 带有 JPA : default constructor hell 的 Ko

asynchronous - Kotlin协程中的launch/join和async/await有什

random - 如何在 Kotlin 中获取随机数?

java - Java 的 String[] 的 Kotlin 等价物是什么?

kotlin - 在 Kotlin 中试用资源

android-studio - Unresolved reference : kotlinx