Introduce String.toByte()
#KT-8833 Fixed
This commit is contained in:
@@ -333,6 +333,12 @@ public fun String.toUpperCase(locale: java.util.Locale): String = (this as java.
|
||||
*/
|
||||
public fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a signed [Byte] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
public fun String.toByte(): Byte = java.lang.Byte.parseByte(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a [Short] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
|
||||
@@ -27,6 +27,18 @@ class StringJVMTest {
|
||||
assertEquals("", ns.orEmpty())
|
||||
}
|
||||
|
||||
@test fun toBoolean() {
|
||||
assertEquals(true, "true".toBoolean())
|
||||
assertEquals(true, "True".toBoolean())
|
||||
assertEquals(false, "false".toBoolean())
|
||||
assertEquals(false, "not so true".toBoolean())
|
||||
}
|
||||
|
||||
@test fun toByte() {
|
||||
assertEquals(77.toByte(), "77".toByte())
|
||||
assertFails { "255".toByte() }
|
||||
}
|
||||
|
||||
@test fun toShort() {
|
||||
assertEquals(77.toShort(), "77".toShort())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user