Conversion between a string and other data types in Kotlin
This article explores different ways to convert a string to all other data types supported by Kotlin.
1. Convert String to Int
You can use the toInt() function to get the corresponding int value of a string. If the string is not a valid representation of a number, NumberFormatException would be thrown.
|
1 2 3 |
fun toInt(s: String): Int { return s.toInt() } |
2. Convert String to Float
You can use the toFloat() function to get the corresponding float value from a String. If it cannot convert the string to a float, NumberFormatException will be thrown.
|
1 2 3 |
fun toFloat(s: String): Float { return s.toFloat() } |
3. Convert String to Double
You can use the toDouble() function to get the corresponding double value. If it cannot convert the string to a double, NumberFormatException will be thrown.
|
1 2 3 |
fun toDouble(s: String): Double { return s.toDouble() } |
4. Convert String to Long
You can use the toLong() function to parse the string as a long number. If the string is not a number, NumberFormatException will be thrown.
|
1 2 3 |
fun toLong(s: String): Long { return s.toLong() } |
5. Convert String to Short
You can use the toShort() function to get the corresponding short value. If it cannot convert the string to a short, NumberFormatException will be thrown.
|
1 2 3 |
fun toShort(s: String): Short { return s.toShort() } |
6. Convert String to Boolean
You can use the toBoolean() to get the Boolean value represented by the specified string. The function returns true if the specified string is equal to “true” (case ignored) and false otherwise.
|
1 2 3 |
fun toBoolean(s: String): Boolean { return s.toBoolean() } |
7. Convert String to Character Array
To convert a string to a character array containing the characters in the string, you can use the toCharArray() function.
|
1 2 3 |
fun toCharArray(s: String): CharArray { return s.toCharArray() } |
8. Convert String to Byte Array
To convert a string to a byte array, you can use the getBytes() function, which encodes the string’s contents into a sequence of bytes using default or specified encoding.
|
1 2 3 |
fun toByteArray(s: String): ByteArray { return s.toByteArray() } |
9. Convert String to Byte
You can parse a string as a signed Byte number using the toByte() function. It throws NumberFormatException if the string is not a valid representation of a number.
|
1 2 3 |
fun toByte(s: String): Byte { return s.toByte() } |
10. Convert String to BigDecimal
Finally, you can also convert a string to a BigDecimal using the toBigDecimal() function. It throws NumberFormatException if the string is not a valid representation of a number.
|
1 2 3 4 5 |
import java.math.BigDecimal fun toBigDecimal(s: String): BigDecimal { return s.toBigDecimal() } |
That’s all about conversion between a string and other data types in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)