如何在Kotlin中将String转换为Long? [英] How to convert String to Long in Kotlin?

查看:1809
本文介绍了如何在Kotlin中将String转换为Long?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,由于缺少类似Long.valueOf(String s)的方法,我陷入了困境.

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

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

How to convert String to Long in Kotlin?

推荐答案

1. string.toLong()

将字符串解析为[Long]数字并返回结果.

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

@如果字符串无效,则抛出NumberFormatException 数字的表示形式.

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

2. string.toLongOrNull()

将字符串解析为[Long]数字并返回结果或null 如果字符串不是数字的有效表示形式.

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)

将字符串解析为[Long]数字并返回结果.

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

@如果字符串无效,则抛出NumberFormatException 数字的表示形式.

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

@throws抛出IllegalArgumentException 对于字符串到数字的转换,[radix]不是有效的基数.

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

将字符串解析为[Long]数字并返回结果或null 如果字符串不是数字的有效表示形式.

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 到数字转换.

@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中将String转换为Long?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆